Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex negative look-behind

UPD: I need to check if @if(Medellin) so take "City Medellin" but if @if(null) don’t take anything

Here is my pattern:

(?<!@if\(null\)).*?@end$

Testing strings:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@if(Medellin) City Medellin @end

@if(null) City Medellin @end

I understood that first checks right side, and it takes all string 🙁

>Solution :

You can use

@if\((?!null\))\w+\)\s*(.*?)\s*@end\b

See the regex demo. To match across lines, you will need either (?s)@if\((?!null\))\w+\)\s*(.*?)\s*@end\b or @if\((?!null\))\w+\)\s*([\w\W]*?)\s*@end\b.

Details:

  • @if\( – a @if( string
  • (?!null\)) – a negative lookahead that fails the match if there is null) string immediately to the right of the current location
  • \w+ – one or more word chars
  • \) – a ) char
  • \s* – zero or more whitespaces
  • (.*?) – zero or more chars as few as possible
  • \s* – zero or more whitespaces
  • @end\b@end whole word.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading