import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "((.*?)(%20){1,}){2}";
final String string = "SHOULD MATCH:\n"
+ "https://domain.com/search=any%20url%20encoded_word-here\n"
+ "https://domain.com/search=any%20url%20encoded_word-here%20\n"
+ "https://domain.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty\n"
+ "https://domain.com/search=z%C5%82oty%20z%C5%82ota%20%C5%82ata\n"
+ "https://domain.com/search=any%20%20word%20%20here\n"
+ "https://domain.com/search=any%20word%20here&color=blue\n"
+ "https://domain.com/search=any-1st%20word_2nd%20here3\n\n"
+ "SHOULD NOT MATCH:\n"
+ "https://domain.com/search=one%20two%20three%20four\n"
+ "https://domain.com/search=one%20%20two%20%20three%20%20four\n"
+ "https://domain.com/search=one%20%20two%20three%20%20four\n"
+ "https://domain.com/search=one%20%20two%20%20three%20%20four\n"
+ "https://domain.com/search=one%20two%20three%20four&color=blue\n"
+ "https://domain.com/search=z%C5%82oty%20z%C5%82oty%20z%C5%82oty%20word\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html