I'm trying to match an expression under the cursor.

In this case it happens to be reStructuredText, however I don't think that's such an important detail.

:role:`Some Content`

While this works, it depends on the cursor being at the beginning of the expression.

(search-forward-regexp "\\(\\:[a-zA-Z0-9_]+\\:`.+?`\\)" nil t 1)

Since there could be multiple matching expressions on a single line, what's a reliable way to find an expression under the cursor, where the cursor can be anywhere between the start/end of the expression.

1 Answer 1


You can use thing-at-point-looking-at to get the bounds of the match:

(when (thing-at-point-looking-at "\\(\\:[a-zA-Z0-9_]+\\:`[^`]+`\\)")
  (buffer-substring (match-beginning 0) (match-end 0)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.