我想操作一个字符串,我的字符串是:
String string = "dfwx--05-dfpixc3"
我希望我的输出如下:
dfwx--05
dfpixc3
所以分隔符是 -
而不是 --
此外,我可以使用 Matcher 类 Pattern 类解决这个问题吗?
最佳答案
您可以使用 split 与此正则表达式 \b-\b
与 Word Boundaries像这样:
\b
represents an anchor like caret (it is similar to$
and^
) matching positions where one side is a word character (like\w
) and the other side is not a word character (for instance it may be the beginning of the string or a space character).
String[] split = string.split("\\b-\\b");
<小时/>
Because my String is variable There always a number after --
另一个解决方案可能是使用正向回顾,就像这样 (?<=\d)-
:
String[] split = string.split("(?<=\\d)-");
<小时/>
输出
[dfwx--05, dfpixc3]
关于java - 正则表达式 : Using Regex in Java 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59460482/