Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17486

Regex containing negative lookahead is matching entire string

I have this text:

Name: <% $person %> <& component &>

and i'm trying to come up with a Ruby regex that will give me everything that doesn't look like <% .. %> or by <& ... &>.

This is my approach using negative lookahead:

(?!<&|<%)(.+\s*)(?!&>|%>)

but it returns the whole string (tested here), when I expected it to return Name:.

Upvotes: 1

Views: 505

Answers (2)

Valdi_Bo
Valdi_Bo

Reputation: 30971

Try this regex:

  • (?<=^|[&%]>) - Positive lookbehind: Either start of text or a "closing" marker (&> or %>).
  • (.+?) - This is what you want to capture. Note the ? after +, the reluctant version, preventing from "consuming" the whole rest of text.
  • (?=<[&%]|$) - Positive lookahead: Either an "opening" marker (<& or <%) or end of text.

To sum up:

(?<=^|[&%]>)(.+?)(?=<[&%]|$)

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163277

Maybe it is an option for you to replace everything in your string that is between <% .. %> or <& ... &> with an empty string:

(?:<%[^%]+%>|<&[^&]+&>)

Demo

Upvotes: 2

Related Questions