import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "commit[\\s\\S]+?(?=commit [a-z0-9]+|$)";
final String string = "commit 7200304f2e965e348dfa362a4adf80e4e0a3d621 (HEAD -> master, tag: v0.5.0.1, origin/master, origin/HEAD)\n"
+ "Author: Jonathan Dance <jd@wuputah.com>\n"
+ "Date: Mon May 21 11:02:39 2018 -0700\n\n"
+ " 0.5.0.1\n\n"
+ "commit 7314c4a5f5c7502babe45fdd98e65c7210fbf92b\n"
+ "Author: Jonathan Dance <jd@wuputah.com>\n"
+ "Date: Mon May 21 11:01:31 2018 -0700\n\n"
+ " add task to build with chmod -R beforehand\n"
+ " \n"
+ " fixes #129\n\n\n"
+ "commit 7314c4a5f5c7502babe45fdd98e65c7210fbf92b\n"
+ "Author: Jonathan Dance <jd@wuputah.com>\n"
+ "Date: Mon May 21 11:01:31 2018 -0700\n\n"
+ " add task to build with chmod -R beforehand\n"
+ " \n"
+ " fixes #129";
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