【发布时间】:2011-09-16 11:21:03
【问题描述】:
由于String.split() 与正则表达式一起使用,所以这个 sn-p:
String s = "str?str?argh";
s.split("r?");
...产生:[, s, t, , ?, s, t, , ?, a, , g, h]
在r? 序列上拆分此字符串以生成[st, st, argh] 的最优雅方法是什么?
编辑:我知道我可以逃避有问题的?。问题是我不知道分隔符,而且我不想通过编写 escapeGenericRegex() 函数来解决这个问题。
【问题讨论】:
-
这在How to split a string in Java - Stack Overflow的接受答案中有所提及。相关问题:如何通过(space)/(backslash)/(newline)/(pipe)拆分字符串? ; How to escape text for regular expression in Java