regex - How to split comma-separated string but exclude some words containing comma in Java -
assume have below string:
"test01,test02,test03,exceptional,case,test04"
what want split string string array, below:
["test01","test02","test03","exceptional,case","test04"]
how can in java?
this negative lookaround regex should work you:
(?<!exceptional),|,(?!case)
working demo java code:
string[] arr = str.split("(?<!exceptional),|,(?!case)");
explanation:
this regex matches comma if 1 of these 2 conditions meet:
comma not preceded wordexceptional
using negative lookbehind (?<!exceptional)
comma not followed word case
using negative lookahead (?!case)
that disallows splitting on comma when surrounded exceptional
, case
on either side.
java regex string
No comments:
Post a Comment