2
votes

Using Emacs in evil-mode, I'm getting problems with regular expressions that include \_, like e.g.

/TAG1\_.\{-}TAG2
/TAG1\_s*TAG2

For example, the first regular expressión is used in Vim to find any block of text between TAG1 and TAG2, including blank spaces and new lines. It will match e.g.

TAG1
text

more text TAG2

However, in Emacs evil-mode, as soon as I type \_, I get a Premature end of regular expression error message. Other searches without \_, as

/TAG1\n*TAG2

work as expected, so the problem is specifically with \_. I've tried to play a bit with the evil group settings, but to no avail. Any hint where the problem could lie?

P.S.: The issue takes place regardless of whether I use the GUI or not. In Vim I don't get any kind of problems

2
What does it match when the expression is just \_ ?user557597
- doesn't normally need to be escaped. Some homemade engines use features like this to mean meta (example \< is a beginning of word boundry)user557597
When the expression is just /\_ it says Incomplete input. If _ is used without escaping, it is treated as any other character, as expected: e.g. /TAG1_s*TAG2 finds the string TAG1_sTAG2 (but I want to find is TAG1 TAG2!)summer

2 Answers

3
votes

This is not a problem with evil mode, in emacs regular expressions \ has a special meaning. From the emacs manual

\ has two functions: it quotes the special characters (including ‘\’), and it introduces additional special constructs.

Because ‘\’ quotes special characters, ‘\$’ is a regular expression that matches only ‘$’, and ‘[’ is a regular expression that matches only ‘[’, and so on.

You will have to escape the \, so to search for TAG\ you will need to enter the regexp TAG\\

You may find this page useful for learning about elisp regular expressions.

1
votes

If, as you say in your reply to @Iqbal, you are trying to match TAG1, followed by zero or more space chars or newline chars, followed by TAG2, then in Emacs a regexp for that is written this way: TAG1[ \n]*TAG2.

And yes, you owe it to yourself to read the regexp section of the manual. Different languages handle regexps differently, and Emacs Lisp is no exception to that rule.