I want to Split a String delimited by ‘|’. But want to ignore a String value that has ‘|’.
Find below example:
String s = "Shashank|Sam|Location|20246|India|City in India|USA Country|Location|India Specific 2021|25236|A";
I want to consider Location|India Specific 2021 as one value.
Expected Output:
Shashank
Sam
Location
20246
India
City in India
USA Country
Location|India Specific 2021
25236
A
Whenever I find Location|India in String, I want to ignore | in that.
I tried using
(?<!Location|India)\|
But output is
Shashank
Sam
Location|20246
India
City in India
USA Country
Location|India Specific 2021
25236
A
Thanks
>Solution :
You can use this regex with a negative lookahead with a nested lookbehind:
\|(?!(?<=Location\|)India )
RegEx Details:
\|: Match a|(?!: Start negative lookahead(?<=Location\|): Make sure that we haveLocation|before current positionIndia: MatchIndia
): End negative lookahead