$re = '/^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\\\_\(ツ\)_\/¯)(?<scope>\(\w+\)?((?=:\s)|(?=!:\s)))?(?<breaking>!)?(?<subject>:\s.*)?|^(?<merge>Merge \w+)/m';
$str = '## Should Pass
build: test
chore(topic): descriptoin
chore(junk): that\'s better
¯\\_(ツ)_/¯: descriptoin
feat: some with-hyphen
### should pass with breaking change
feat!: monkeys are the best for animal testing
build(three)!: test
Merge words
### Failed to match test
Merge
monkey(dog): no
junk
### Partials that should fail
build(with colon): must include colon. <<< fails number four
build(one) <<< must have descriptoin and must have colon
feat!:dogs are better for animal testing << give me your whitespace
build <<< thould fail, not allowed end of line
build(: <<< should fail
build(words):noSPace <<< should fail
build(two)asdfadfadasdf_this is a failure
build(<> : dda!sd): test <<< should this fail?
build(example:module)!: test subject <<< that should fail too
¯\\_(ツ)_/¯[type]:descriptoin
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
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 PHP, please visit: http://php.net/manual/en/ref.pcre.php