import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "car (zap(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|yes(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|xerox(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|with(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|view(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|under(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|this(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|sort(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|rest(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|quick|(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|orange(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|nope|(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|letter(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|kayak(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|joseph(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|indiana(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|humus(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|gelatin(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|frankfurter(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|elephant(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|doormat(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|carrot(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|banana(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?|apple(?: zap| yes| xerox| with| view| under| this| sort| rest| quick| pie| orange| nope| meal| letter| kayak| joseph| indiana| humus| gelatin| frankfurter| elephant| doormat| carrot| banana| apple)?) crashed";
final String string = "car apple view crashed\n"
+ "car doormat elephant crashed\n"
+ "car humus zap crashed\n"
+ "car banana crashed\n"
+ "car crashedddd\n"
+ "^ the above shouldn't matchd";
final Pattern pattern = Pattern.compile(regex);
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