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
Drew
  • 75,699
  • 9
  • 109
  • 225
user1683620
  • 103
  • 6

1 Answers1

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.

phils
  • 48,657
  • 3
  • 76
  • 115
  • 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. – user1683620 Jan 18 '21 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 '21 at 19:59