import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(.*\\d*[13579]\\.article)|(.*[a-d|[f-m]$)";
final String string = "*** Match odds or A-M\n"
+ "(.*\\d*[13579]\\.article)|(.*[a-d|[f-m]$)\n"
+ "https://edu.rsc.org/science-research/salt-crystal-grows-legs/4012581.article\n"
+ "edu.rsc.org/45.article\n"
+ "edu.rsc.org/weuvkee/a\n"
+ "edu.rsc.org/weuvkee/f\n\n\n"
+ "****** Don't match\n"
+ "https://edu.rsc.org/ideas/5-ways-to-explain-titration/4012500.article?utm_source=house-list&utm_medium=email&utm_campaign=monthly-alert\n"
+ "https://edu.rsc.org/feature/making-materials-from-biomass/4012606.article\n"
+ "https://edu.rsc.org/eic/classroy\n"
+ "https://edu.rsc.org/wibble/chips/stuff/science\n"
+ "edu.rsc.org\n\n\n\n"
+ "*** Match evens or N-Z\n"
+ "(.*\\d*[02468]\\.article)|(.*[n-z]$)\n"
+ "https://edu.rsc.org/ideas/5-ways-to-explain-titration/4012500.article?utm_source=house-list&utm_medium=email&utm_campaign=monthly-alert\n"
+ "https://edu.rsc.org/feature/making-materials-from-biomass/4012606.article\n"
+ "https://edu.rsc.org/eic/classroy\n"
+ "https://edu.rsc.org/wibble/chips/stuff/science\n\n"
+ "****** Don't match\n"
+ "https://edu.rsc.org/science-research/salt-crystal-grows-legs/4012581.article\n"
+ "edu.rsc.org/45.article\n"
+ "edu.rsc.org/weuvkee/a\n"
+ "edu.rsc.org/weuvkee/f";
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