Regex for Git commit message

I am trying to create a regex to force Git commit messages to conform to a specific format. I've been banging my head against the keyboard while modifying the semi-working version I have, but I just can't get it to work the way I want it to. Here's what I have now:

/^([a-z]{2,4}-[\d]{2,5}[, \n]{1,2})+\n{1}^[\w\n\s\*\-\.\:\'\,]+/i


Here is the text I'm trying to accomplish:

AB-1432, ABC-435, ABCD-42

Here is the multiline description, following a blank 
line after the Jira issue IDs
- Maybe bullet points, with either dashes
* Or asterisks


It currently matches this, but it will also match if there is no blank line after the problem IDs, and if there are multiple blank lines after that.

Is there anyway to provide this, or do I just need to live with it?

It's also pretty ugly, I'm sure there is a shorter way to write this.



source to share

1 answer

Your regex allows \n

as one of the possible characters after the newline you want, so its a match when multiple.

Here's the cleaned up regex:

/^([a-z]{2,4}-\d{2,5}(?=[, \n]),? ?\n?)+^\n([-\w\s*.:',]+\n)+/i


  • This requires at least one character [-\w\s*.:',]

    before the next newline.
  • I changed the problem IDs to have one possible comma, space and newline in that order (up to one of them). Can you use lookaheads? If so, I've added (?=[, \n])

    to make sure the problem ID is accompanied by at least one of these characters.
  • Also note that many characters do not need escaping in the character class.


All Articles