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

Ignore pattern in split()

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:

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

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 Demo

RegEx Details:

  • \|: Match a |
  • (?!: Start negative lookahead
    • (?<=Location\|): Make sure that we have Location| before current position
    • India : Match India
  • ): End negative lookahead
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