Regex pattern to check JIRA ticket numbers before specific delimiter in commit messages

I need help with a regex pattern that can validate JIRA ticket IDs in git commit messages. Right now I’m working with this pattern: [A-Z][A-Z_0-9]+-[0-9]

Here’s an example of what my commit messages look like:

PROJ-123 TASK-456 BUG-789:: some description here with MISC-999 inside

The problem is I only want to match the JIRA IDs that appear before the :: separator. So in the example above, I want to capture PROJ-123, TASK-456, and BUG-789 but ignore MISC-999 since it comes after the :: delimiter.

How can I modify my regex to only validate ticket numbers in the first part of the commit message before the double colon?

use ([A-Z][A-Z_0-9]+-[0-9]+)(?=.*::) to grab all JIRA IDs before ::. you might wanna split the string first tho - it’ll save u headaches if the format gets weird later.

To match only the part before the :: in commit messages, try ^[A-Z][A-Z_0-9]+-[0-9]+(?=::). This pattern anchors to the start and captures JIRA IDs before the delimiter. Alternatively, you can split the commit message at :: first, then apply your original regex on the first segment. This approach often results in cleaner code and better accommodates various commit formats.

You could try a non-greedy match with your pattern: ([A-Z][A-Z_0-9]+-[0-9]+)(?=.*?::) might work. But honestly, I’d just extract everything before the :: first with string operations, then run your original regex on that. Regex-only solutions get messy when commit formats vary between teams or people forget the delimiter. Plus it’s way easier to debug string splitting than complex lookaheads when things break.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.