如果我有一个字符串
“” word3 word2 word3 word4 word5 word3 word7 word8 word9 word10“
我想找到所有“ word3”,使其在“ word5”的3个词之内,
我将匹配“ word3”的第二次和第三次出现
我将使用什么正则表达式或逻辑?我有两种方法来解决这个问题,但是对我来说它们似乎效率极低。
最佳答案
您没有定义单词,因此我将其视为单词字符序列,这是一种通过拆分String
进行迭代而无需专门使用正则表达式的方法:
String str = "word3 word2 word3 word4 word5 word3 word7 word8 word9 word10";
String[] words = str.split("\\W+");
for (int i = 0; i < words.length; i++) {
// Iterate in an inner loop for nearby elements if "word5" is found.
if (words[i].equals("word5"))
for (int j = Math.max(0, i - 3); j < Math.min(words.length, i + 3); j++)
if (words[j].equals("word3")) {
// Do something with words[j] to show that you know it exists.
// Or use it right here instead of assigning this debug value.
words[j] = "foo";
}
}
// Prints the result.
for (final String word : words)
System.out.println(word);
Code Demo标准输出:
word3
word2
foo
word4
word5
foo
word7
word8
word9
word10
否则,这是正则表达式替换:
Pattern pattern = Pattern.compile("word3(?=(?:\\W*\\w++){0,2}?\\W*+word5)|(word5(?:\\W*\\w++){0,2}?\\W*+)word3");
Matcher matcher;
String str = "word3 word2 word3 word4 word5 word3 word7 word8 word9 word10";
while ((matcher = pattern.matcher(str)).find())
// Do something with matcher.group(1) to show that you know it exists.
// Or use it right here instead of replacing with this empty value.
str = matcher.replaceFirst(matcher.group(1) == null ? "" : matcher.group(1));
System.out.println(str);
但是,尽管此正则表达式有效,但替换第三个单词
word3
认为第一个单词word3
可以替换掉,这就是为什么不使用regex的原因。Code Demo标准输出:
word2 word4 word5 word7 word8 word9 word10
要进行此项工作的小修改是:
str = matcher.replaceFirst((matcher.group(1) == null ? "" : matcher.group(1)) + "baz");