Reputation: 2645
if I have a string "word3 word2 word3 word4 word5 word3 word7 word8 word9 word10"
And I want to find all "word3" such that it is within 3 words of "word5", I would get a match to the second and third occurrences of "word3"
what regex expression or logic would I use? I had 2 ways I was going to approach it, but they seem incredibly inefficient to me.
Upvotes: 0
Views: 520
Reputation: 11041
You did not define a word, hence I will take this as a word character sequence, and here is an approach without exclusively using regexes, by iterating through splitting the 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 STDOUT:
word3 word2 foo word4 word5 foo word7 word8 word9 word10
Otherwise, here's the regex replacement:
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);
However while this regex works, replacing away the third word word3
deemed the first word word3
able to be replaced away, which is why regex is not the way to go for this.
Code Demo STDOUT:
word2 word4 word5 word7 word8 word9 word10
Small modification to make this work would be:
str = matcher.replaceFirst((matcher.group(1) == null ? "" : matcher.group(1)) + "baz");
Upvotes: 1