import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^[a]\\w+";
final String string = "\n"
+ "a dance high our spouse\n"
+ "able danger hill out spring\n"
+ "about dark him outside square\n"
+ "above daughter himself over stairs\n"
+ "accident day hint own stairway\n"
+ "acid decide his oxygen stand\n"
+ "across decided history page stars\n"
+ "act decimal hold paint start\n"
+ "add deep hole pair state\n"
+ "admission delivery home pants statement\n"
+ "Africa dentist hope paper stay\n"
+ "after deposit horse paragraph step\n"
+ "again describe hospital parents stick\n"
+ "against desert hot park still\n"
+ "age design hotel part stone\n"
+ "ago desk hours party stood\n"
+ "agree destination house passed stop\n"
+ "aide developed how passengers store\n"
+ "air diary however password storm\n"
+ "alarm dictionary huge past story\n"
+ "all did human pattern stove\n"
+ "almost didn't hundred pay straight\n"
+ "alone died husband payment strange\n"
+ "already difference I pedestrians street\n"
+ "also different I'll pen strong\n"
+ "although digital ice pencil students\n"
+ "always diner idea people study\n"
+ "am dinner if per subject\n"
+ "ambulance direct important period subtract\n"
+ "America direction in perishable subway\n"
+ "amount directions inches person such";
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