Reputation: 23
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
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