Home > Net >  Lookahead and lookbehind with regex
Lookahead and lookbehind with regex

Time:06-07

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

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.

CodePudding user response:

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

  • Related