0

I am starting regex with Emacs and cannot get the right regex. I would like to match the line 1 and 4 in the image below, where variable-pitch is "active" and do not match 2 and 3 where is "inactive"

so I am basically trying to say :

line with 'variable-pitch' not containing with ";" or "|"

enter image description here

Following this post, I switched re-builder to string mode.

Edit : Following the different comments

  • here is the text if you want to try
  • i changed the the screenshot with re-builder actually set to string
4
  • Anchor your regex.
    – choroba
    Jan 18 at 9:34
  • 1
    If you also posted that content as plain text, people could test against it.
    – phils
    Jan 18 at 9:35
  • @phils : done sharing, see edit Jan 18 at 12:26
  • @user1683620: You can install ix from elpa (package-install 'ix). Just M-x ix and you get a link to a pastebin on your buffer. Very fast and convenient to transmit text
    – gigiair
    Jan 18 at 18:12
0

I switched re-builder to string mode

Did you? You've used \\(...\\) to (apparently successfully) match a sub-group, which indicates you're using read syntax. In string syntax that would be \(...\).


Regarding this:

[^;|\\|].*\\(variable-pitch.*\\)

[^;|\\|] says to match anything other than ;, |, \, or (again) |. Note also that newlines are not excluded.

Following that you match .*, which is zero or more non-newline characters.

Finally you match a subgroup consisting of variable-pitch followed by zero or more non-newline characters.


so I am basically trying to say: line with 'variable-pitch' not containing with ";" or "|"

So something like this?

"^[^;|\n]*variable-pitch[^;|\n]*$"

Note that you can only use \n in read syntax (because it's an escape sequence for reading strings, not an escape sequence for regexps). If you're using string syntax in re-builder then you'll need to type a literal newline instead.

2
  • Thanks for you help. I tested your proposition and updated the screenshot accordingly. It looks like it matches anyway line 2 et 3 whereas it should not. Jan 18 at 12:26
  • Whoops. Despite having mentioned this, I also forgot to exclude newlines from my character alternatives. I've updated the pattern.
    – phils
    Jan 18 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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