我写了一个简单的正则表达式
String s = "#!key1 #!compound.key2 #!super.compound.key3";
Matcher matcher = Pattern.compile("(?<=#!)(\\w+\\.*\\w+)+").matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
结果
实际
key1
compound.key2
super.compound
我想知道为什么它与 super.compound
匹配,但与我预期的 super.compound.key3
不匹配。
预期
key1
compound.key2
super.compound.key3
对正则表达式的任何改进都将受到欢迎。
最佳答案
您需要使用
(?<=#!)\w+(?:\.\w+)*
请参阅regex demo和正则表达式图:
String s = "#!key1 #!compound.key2 #!super.compound.key3";
Matcher matcher = Pattern.compile("(?<=#!)\\w+(?:\\.\\w+)*").matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
// => [key1, compound.key2, super.compound.key3]
关于java - (?<=#!)(\\w+\\.*\\w+)+ 与 #!super.compound.key3 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57568460/