2

I would like to match lines that have the phrase a test but not start with >. So I want to match

This is a test
a test this is

but not

> This is a test

I am not sure how to do this without lookahead in emacs.

As a first try I tried^[^>].*a test. However, for some reason, this matches > This is a test when there's a newline before it which I don't understand since the first character in a line should not be >.

What is the correct regular expression for this case? And why is my regular expression matching lines starting with >.

emacs screenshot

2 Answers 2

3

The point is that the construct [^>] matches even a newline as stated in https://www.emacswiki.org/emacs/RegularExpression. Indeed such regex matches even the line above. So replacing it with [^>\n] solves the problem.

0

Your problem must be something else; that regex looks fine to me. In fact, I used M-x regexp-builder to verify that it does work as expected.

1
  • I just tried it with regexp-builder with emacs -Q and I am still getting > This is a test matched (I am adding an image to show this in my post). However, I now understand that the problem seems to be that if there is an empty line before > This is a test then I get the incorrect match (I edited my post to reflect this). Any idea why this is and how to fix it?
    – Tohiko
    Commented Apr 26, 2020 at 9:01

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.