TMtech
TMtech

Reputation: 1166

Regex Match Reset \K Equalent In Java

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.

Upvotes: 1

Views: 350

Answers (2)

The fourth bird
The fourth bird

Reputation: 163467

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:).+(?=....)

Upvotes: 1

blhsing
blhsing

Reputation: 106901

You can use a lookbehind assertion instead:

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

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

Upvotes: 1

Related Questions