如果我在java中运行这个正则表达式,我将接收{ab,de,f},但是我希望接收{ab,bc,de,f}。我认为bc不能被接收,因为bc和ab有重叠的字母。如何更改默认行为?
发布于 2015-02-21 20:56:22
您可以尝试使用零宽度的前瞻机制,因此每次测试都会在执行测试之前将光标重置为位置。
只需遍历字符之间的所有位置,并检查是否存在匹配正则表达式的子字符串。您可以将此子字符串放置在捕获组中,并在以后访问它。
String input = "abcdef";
Pattern p = Pattern.compile("(?=(ab)|(bc)|(de)|(f))");
Matcher m = p.matcher(input);
while (m.find()){
for (int i=1; i<=m.groupCount(); i++){
if (m.group(i)!=null)
System.out.println("group ("+i+") -> "+m.group(i));
}
}输出:
group (1) -> ab
group (2) -> bc
group (3) -> de
group (4) -> fhttps://stackoverflow.com/questions/28651182
复制相似问题