import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\\\_\\(ツ\\)_\\/¯)(?<scope>\\(\\w+\\)?((?=:\\s)|(?=!:\\s)))?(?<breaking>!)?(?<subject>:\\s.*)?|^(?<merge>Merge \\w+)";
final String string = "## Should Pass\n\n"
+ "build: test\n\n"
+ "chore(topic): descriptoin\n"
+ "chore(junk): that's better\n"
+ "¯\\_(ツ)_/¯: descriptoin\n"
+ "feat: some with-hyphen\n\n\n"
+ "### should pass with breaking change\n"
+ "feat!: monkeys are the best for animal testing\n"
+ "build(three)!: test\n\n\n"
+ "Merge words\n\n"
+ "### Failed to match test\n\n"
+ "Merge\n"
+ "monkey(dog): no\n"
+ "junk\n\n"
+ "### Partials that should fail\n"
+ "build(with colon): must include colon. <<< fails number four\n"
+ "build(one) <<< must have descriptoin and must have colon\n"
+ "feat!:dogs are better for animal testing << give me your whitespace\n\n"
+ "build <<< thould fail, not allowed end of line\n"
+ "build(: <<< should fail \n"
+ "build(words):noSPace <<< should fail\n"
+ "build(two)asdfadfadasdf_this is a failure\n"
+ "build(<> : dda!sd): test <<< should this fail? \n"
+ "build(example:module)!: test subject <<< that should fail too\n"
+ "¯\\_(ツ)_/¯[type]:descriptoin\n";
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