我有一个以下字符串:
161544476293,26220\,1385853403,WLAN-EneTec5,-85,0,0
如何用逗号分隔它,但避免\,
个案例。
在上面提到的情况下,字符串应该拆分为:
161544476293
26220\,1385853403
WLAN-EneTec5
-85
0
0
谢谢,
答案 0 :(得分:3)
您可以使用negative lookbehind:
str.split("(?<!\\\\),");
// OUTPUT: "161544476293", "26220\,1385853403", "WLAN-EneTec5", "-85", "0", "0"
(?<!\\\\) Negative Lookbehind - Assert that it is impossible to match the regex below
\\ matches the character \ literally
, matches the character , literally
答案 1 :(得分:3)
这样的模式:
(?<!\\),
将匹配任何,
字符而非,后面紧跟\
个字符。当然,这是Java,所以请确保你在字符串文字中转义\
:
String pattern = "(?<!\\\\),";