[英]Reusing the consumed characters in pattern matching in java?
Consider the following Pattern :- 考虑以下模式:
aba
And the foll. 还有傻瓜。 source string :-
源字符串:-
abababbbaba
01234567890 //Index Positions
Using Pattern and Matcher classes from java.util.regex package, finds this pattern only two times since regex does not consider already consumed characters. 使用java.util.regex包中的Pattern和Matcher类,只能发现此模式两次,因为regex不会考虑已经消耗的字符。
What if I want to reuse a part of already consumed characters. 如果我想重用一部分已经消耗的字符怎么办。 That is, I want 3 matches here, one at position 0, one at 2 (which is ignored previously), and one at 8.
也就是说,我要在这里进行3场比赛,一场在位置0,一场在2(之前已被忽略),一场在8。
How do I do it?? 我该怎么做??
You can use a look ahead for that. 您可以为此使用前瞻性 。 Now what you have is the first position in
group(1)
and the second match in group(2)
. 现在,你拥有的是在第一位置
group(1)
并在第二场比赛group(2)
Both making each String of length 3 in the sentence you are searching in. 两者都会使您要搜索的句子中每个长度为3的字符串。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Question8968432 {
public static void main(String args[]) {
final String needle = "aba";
final String sentence = "abababbbaba";
final Matcher m = Pattern.compile("(.)(?=(..))").matcher(sentence);
while (m.find()) {
final String match = m.group(1) + m.group(2);
final String hint = String.format("%s[%s]%s",
sentence.substring(0, m.start()), match,
sentence.substring(m.start() + match.length()));
if (match.equals(needle)) {
System.out.printf("Found %s starting at %d: %s\n",
match, m.start(), hint);
}
}
}
}
Output: 输出:
Found aba starting at 0: [aba]babbbaba
Found aba starting at 2: ab[aba]bbbaba
Found aba starting at 8: abababbb[aba]
You can skip the final String hint
part, this is just to show you what it matches and where. 您可以跳过
final String hint
部分,这只是向您显示它匹配的内容和位置。
如果您可以更改正则表达式,则可以简单地使用以下命令:
a(?=ba)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.