Home > Enterprise >  Regex Match Reset \K Equalent In Java
Regex Match Reset \K Equalent In Java

Time:09-17

I have come up with a regex pattern to match a part of a Json value. But only PRCE engine is supporting this. I want to know the Java equalent of this regex.

Simplified version

cif:\K.*(?=(. ?){4})

Matches part of the value, leaving the last 4 characters.

cif:test1234

Matched value will be test

https://regex101.com/r/xV4ZNa/1

Note: I can only define the regex and the replace text. I don't have access to the Java code since it's handle by a propriotery log masking framework.

CodePudding user response:

You can use a lookbehind assertion instead:

(?<=cif:).*(?=(. ?){4})

Demo: https://regex101.com/r/xV4ZNa/3

CodePudding user response:

You can write simplify the pattern to:

(?<=cif:).*(?=....)

Explanation

  • (?<=cif:) Positive lookbehind, assert cif: to the left
  • .* Match 0 times any character without newlines
  • (?=....) Positive lookahead, assert 4 characters (which can include spaces)

See a regex demo.

If you don't want to match empty strings, then you can use . instead

(?<=cif:). (?=....)
  • Related