2

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

summer
  • 121
  • 1
  • 4
  • What does it match when the expression is just `\_` ? –  Feb 18 '14 at 18:43
  • `-` doesn't normally need to be escaped. Some homemade engines use features like this to mean meta (example `\<` is a beginning of word boundry) –  Feb 18 '14 at 18:48
  • 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 Feb 18 '14 at 19:30

2 Answers2

3

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.

  • Thank you for your answer, Iqbal. However, I am NOT searching for `TAG\ `. A regex like `/TAG1\_s*TAG2` in Vim tries to find the sequence `TAG1TAG2` where `TAG1` and `TAG2` might be separated by any number of blank spaces or new lines. This is the behaviour that I want in evil-mode... and it is not achieved by escaping again, i.e. `/TAG1\\_s*TAG2` is not working. I have edited my original question, for clarity. Thanks again for the link, I am looking at it, maybe I will find the solution there. – summer Feb 19 '14 at 00:05
  • @summer To match newline in search-replace you will need to type C-q C-j so the regexp can be `TAG1[[:space:]^J]*TAG2` (press C-q C-j to enter ^J) –  Mar 08 '14 at 02:52
1

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.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • 1
    Thank you for your answer @Drew. Of course it is not that I wanted to save myself the bother of reading the Emacs regexp section. The confussion was just that I thought that, because I was using evil-mode, I had to handle regexes exactly as in Vim (I'm a pretty new Emacs user). I see this is not the case, and I thank both you and Iqbal for pointing me in the right direction. – summer Feb 19 '14 at 04:11
  • 1
    No problem; I understand. In general, Emacs has its own way of doing things -- sometimes for excellent reasons, sometimes mainly for "hysterical raisins". But one thing Emacs is quite good at is helping you understand it. My first advice to anyone getting to know Emacs is this: ***Ask Emacs***. For almost 40 years it has touted itself as "*self-documenting*", proof that explaining itself is something that it at least takes seriously. And it usually does a pretty good job of it. The gateway is learning what's available from `C-h C-h` and how to get around in the two Emacs manuals. Enjoy. – Drew Feb 19 '14 at 07:08