java - 正则表达式 : Using Regex in Java 8

标签 java regex string

我想操作一个字符串,我的字符串是:

 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/

相关文章:

string - 从文本中提取特定字符串

Java 文字字符串 : wrong charset at runtime

带有 "\n"的 Java 字符串索引

java - 在没有 plugin.xml 的情况下使用 Eclipse 装饰器

Java方法应该缓存结果

java - java中正则表达式的模式匹配

python - 如何获取以数字开头作为行首并以 5 位数字结尾的字符串

c# - 非常简单的正则表达式不起作用

java - 如何获取多维数组中对象的索引?

java - "The Bean Validation API is on the classpath but no implementation could be found"阻止启动