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

Lookahead and lookbehind with regex

I am trying to build a regex pattern and I’m a beginner.

The string looks like this

INITIAL TEXT\KEYWORD1\TEXT1\KEYWORD2\TEXT2\KEYWORD3\TEXT3

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

The string starts with initial text but the keywords with their texts could be in any order or may not be present.
The initial text could contain any character including backslashes.

I want to capture the initial text so I tried something like this

(?<=(.*)(?=\KEYWORD1\|\KEYWORD2\|KEYWORD3).*)

I am able to capture it on regex101 in group1 but my java code doesn’t recognize the group 1.

Thanks for helping.

>Solution :

If the string starts with the text you want to capture, then you can use a start-of-string anchor followed by a lazy match on any character, terminating with a forward lookahead to one of the keywords:

^.*?(?=\\(?:KEYWORD1|KEYWORD2|KEYWORD3)\\|$)

This will match only the INITIAL TEXT

Demo on regex101

Note that in Java you will need to double the backslash characters in the regex string. Demo on ideone

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