import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "#https://www.native-english.ru/grammar/english-nouns-plural\n\n"
+ "(?<![a-z]) #word boundary\n"
+ "(?: [a-z]+ [bcdfghjklmnpqrtvwzeoya]s\n"
+ " | [a-z]+ (?:[sc]h|[sxoiv])es\n"
+ " | children|men|women|mice|teeth|feet|geese|oxen|deer|sheep|swine\n"
+ ")\n"
+ "$";
final String string = "area\n"
+ "areas\n\n"
+ "child\n"
+ "children\n"
+ "man\n"
+ "men\n"
+ "woman\n"
+ "women\n"
+ "mouse\n"
+ "mice\n"
+ "tooth\n"
+ "teeth\n"
+ "foot\n"
+ "feet\n"
+ "goose\n"
+ "geese\n\n"
+ "ox\n"
+ "oxen\n\n"
+ "deer\n"
+ "sheep\n"
+ "swine\n\n"
+ "cat\n"
+ "cats\n"
+ "bus\n"
+ "buses\n"
+ "moon\n"
+ "moones\n\n"
+ "class\n"
+ "classes\n"
+ "box\n"
+ "boxes\n"
+ "bush\n"
+ "bushes\n"
+ "inch\n"
+ "inches\n"
+ "match\n"
+ "matches\n\n"
+ "shelf\n"
+ "shelves\n"
+ "wive\n"
+ "wives\n\n"
+ "chief\n"
+ "chiefs\n"
+ "roof\n"
+ "roofs\n"
+ "safe\n"
+ "safes\n\n"
+ "army\n"
+ "armies\n"
+ "city\n"
+ "cities\n"
+ "employee\n"
+ "employees\n\n"
+ "day\n"
+ "days\n"
+ "toy\n"
+ "toys\n"
+ "boy\n"
+ "boys\n\n"
+ "hero\n"
+ "heroes\n"
+ "potato\n"
+ "potatoes\n"
+ "tomato\n"
+ "tomatoes\n\n"
+ "solo\n"
+ "solos\n"
+ "radio\n"
+ "radios\n"
+ "photo\n"
+ "photos\n"
+ "piano\n"
+ "pianos";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL | Pattern.COMMENTS | Pattern.CASE_INSENSITIVE);
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