0

https://regex101.com/r/rnr6SC/2

I am writing a regex to find any instances of words that start with & and end with ;. There could be multiple copies on the same line, on different lines, or whatever. I just want to find anything that looks like

&

&dog;

&cat; and &dog;

or similar. I think my check for the ; is too greedy in my example. How do I fix my regex to find the specific words I need without selecting anything else?

ILikeTurtles
  • 989
  • 4
  • 15
  • 47
  • 1
    [`&[^&;]*;`](https://regex101.com/r/YwvufN/1) – Sergey Kalinichenko Feb 15 '18 at 16:15
  • @dasblinkenlight This is exactly what I needed. Works perfect. – ILikeTurtles Feb 15 '18 at 16:20
  • This is not a duplicate of the question linked. I am selecting text nested between two delimiters. I am not dealing with selecting too much. The answer provided in the comments solves my issue. The other two answers appear to be applicable to the question you added as a duplicate. – ILikeTurtles Feb 20 '18 at 16:45

2 Answers2

1

This regex should work for you:

(?<=&)[^;&]+(?=;)

RegEx Demo

  • (?<=&): Lookbehind to assert previous position has &
  • [^;&]+: Match 1+ character that is not ; and not &
  • (?=;): Lookahead to assert next position has ;

Lookarounds just make sure that a matching text is surrounded by & and ;, if you want these markers also to be part of match then remove lookarounds and use:

&[^;&]+;
anubhava
  • 713,503
  • 59
  • 514
  • 593
0

You might remove the lookaheads (?= and make the .* non greedy .*?

&.*?;

That would match:

  • ampersand &,
  • .*? any character zero or more times non greedy
  • a semicolon ;
The fourth bird
  • 127,136
  • 16
  • 45
  • 63