1

I have a problem about regex in Emacs.
For example, there is a string:
abcdef def

I want to write a regex that only matches ‘def’ which follows ‘abc’. That is to say, I just want to capture ‘abcdef’, not ‘def.
And I hope the point can be located between ‘abc’ and ‘def’, like ‘abc^def’. ( ^ stands for point)
I remember there is a way to do it in Perl, but I am not sure if it is feasible in ELISP.

1
  • Note that Emacs regexps have no zero-width look-ahead/behind assertions, which is most likely the Perl regexp feature you were thinking of. Commented Sep 25, 2013 at 2:37

1 Answer 1

3
(defun foo ()
  (when (re-search-forward "abc\\(def\\)" nil nil)
    (goto-char (match-beginning 1))))

But really you should read about regexp matching in Emacs Lisp -- in the Elisp manual, starting at node Regular Expressions. There is little excuse for posting such a basic question without looking for the answer first, yourself.

StackOverflow is for posing and answering programming questions, and you are expected to clearly specfy the problem, show the code you have already used to try to solve it, say what your code does that you do not expect, etc.

People are happy to help, in general, but a minimum of effort is expected from those asking for help.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much @Drew, this is what i want. i am a elisp newbie, :) thank you for you advice
No problem with being a newbie. We are all Elisp newbies, in some areas, at least. Please do read a bit of that manual section, even if you find it heavy-going. It is very helpful, and "getting" regexps in Emacs will greatly improve your use of it (and your fun using it).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.