Java (?<;=#!)(\\w+;\.\.\\w+;)&&x2B;不';不匹配!super.composite.key3
我写了一个简单的regexpJava (?<;=#!)(\\w+;\.\.\\w+;)&&x2B;不';不匹配!super.composite.key3,java,regex,lookbehind,Java,Regex,Lookbehind,我写了一个简单的regexp String s = "#!key1 #!compound.key2 #!super.compound.key3"; Matcher matcher = Pattern.compile("(?<=#!)(\\w+\\.*\\w+)+").matcher(s); while (matcher.find()) { System.out.println(matcher.group()); } 我想知道为什么它与super.component匹配,而不是像我预期
String s = "#!key1 #!compound.key2 #!super.compound.key3";
Matcher matcher = Pattern.compile("(?<=#!)(\\w+\\.*\\w+)+").matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
我想知道为什么它与super.component
匹配,而不是像我预期的那样与super.component.key3
匹配
预期
欢迎对regexp进行任何改进。您需要使用
(?<=#!)\w+(?:\.\w+)*
(?[key1,composite.key2,super.composite.key3]
我想这样写这个正则表达式(?它不匹配,因为您的量化集群在每次迭代开始时查找\w
,而不是
。因此,当它匹配super.component
时,它无法匹配下一个
,因为它需要一个\w
。将您的正则表达式更改为。谢谢,我如何在那里生成正则表达式图?@AndrewTobilko我使用了,但它不支持lookbehinds,我手动编辑了图像。
key1
compound.key2
super.compound.key3
(?<=#!)\w+(?:\.\w+)*
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]