我有一行像 bla_bla**_**test**_1**023
并且想要提取_
和任何下划线之间的单词,后跟数字_digit
,在上面的示例中是test
。
我尝试了以下正则表达式,但不幸的是不起作用:
[^_ ]+(?=[ _\d])
- 它获取“_digit”之前的所有单词,而不仅仅是 _digit
最佳答案
这应该适合你。将Pattern
和Matcher
与环视结合使用。
public static void main(String[] args) {
String word= "bla_bla_test_1023";
Pattern p = Pattern.compile("(?<=_)([^_]+)(?=_\\d+)");
Matcher m = p.matcher(word);
while (m.find()) {
System.out.println(m.group());
}
}
操作:
test
关于java - 正则表达式获取两个下划线之间的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53013824/