bootable86
bootable86

Reputation: 23

How to split string with ',' with a condition in regex java

I want to split a string using regex in java with ',' as delimiter with a condition. it will be a delimiter if only there is one '=' character before next ','.

ex :

    String test = "APPLICATION:1:1=SECTOR,APPLICATION:2:1=INDUSTRY,DESCRIPT:1:1=Sec,tor,DESCRIPT:2:1=Industry,"
    String [] testRegex = test.split(????)

the value of testRegex should be :

     APPLICATION:1:1=SECTOR
     APPLICATION:2:1=INDUSTRY
     DESCRIPT:1:1=Sec,tor
     DESCRIPT:2:1=Industry

is there any idea for the regex? i can't find any example yet till now :(

Upvotes: 1

Views: 155

Answers (1)

yavuzkavus
yavuzkavus

Reputation: 1266

If separator comma should be followed by a part containing equal sign, you may use positive lookahead.

String parts[] = yourString.split(",(?=[^,]*=)");

(?=) is used for positive lookahead. It wont be used for split, it is just an assertion and will be discarded from match.

Upvotes: 1

Related Questions