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 lookbehind

using regex: (?<!map)\s+.collect\(Collectors.toL

To match:

  1. all 2 line strings where the first line does not have "map"
  2. And the second line has collect(Collectors.toL

Use a negative lookbehind, but as you can see in the link below, the second test is also being matched.

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

How do we update so as to match as specified above?

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

>Solution :

You negative lookbehind condition isn’t correct because .map can have many characters before matching .collect. Besides a negative lookbehind with dynamic length isn’t supported in most of regex flavors.

You may use this regex with a negative lookahead:

^(?!\s*\.map).+\n\s*\.collect\(Collectors\.toL

RegEx Demo

Here:

  • ^: Start
  • (?!\s*\.map): Fail the match if we have .map after 0 or more whitespaces
  • .+\n: Match 1+ chars followed by a line break
  • \s*\.collect\(Collectors\.toL: Match your desired text in a new line
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