Regex pattern to match JIRA issue key

Rp Subhub
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2014

https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+keystates that the default pattern for searching commit messages in Stash is

((?<!([A-Z]{1,10})-?)[A-Z]+-\d+)

Can someone please enlighten me why is lookbehind needed here?

2 answers

1 accepted

2 votes
Answer accepted
Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 13, 2014

Hi,

It's to avoid the pattern matching strings like CR-PROJ-1234 (which is a crucible review key, but Bamboo build plan IDs look similar)

Cheers,

Michael

Rp Subhub
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2014

Oh, that makes sense now. Thank you very much!

5 votes
G__Sylvie_Davies__bit-booster_com_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 24, 2016

Blatant pilfering from Stackoverflow (http://stackoverflow.com/questions/19322669/regular-expression-for-a-jira-identifier/30518972#30518972 ):

Official JIRA ID Regex (Java):

Atlassian themselves have a couple webpages floating around that suggest a good (java) regex is this:

((?<!([A-Z]{1,10})-?)[A-Z]+-\d+)

(Source:https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key)

Test String: "BF-18 abc-123 X-88 ABCDEFGHIJKL-999 abc XY-Z-333 abcDEF-33 ABC-1"
Matches: BF-18, X-88, ABCDEFGHIJKL-999, DEF-33, ABC-1

Improved JIRA ID Regex (Java):

But, I don't really like it because it will match the "DEF-33" from "abcDEF-33", whereas I prefer to ignore "abcDEF-33" altogether. So in my own code I'm using:

((?<!([A-Za-z]{1,10})-?)[A-Z]+-\d+)

Notice how "DEF-33" is no longer matched:

Test String: "BF-18 abc-123 X-88 ABCDEFGHIJKL-999 abc XY-Z-333 abcDEF-33 ABC-1"
Matches: BF-18, X-88, ABCDEFGHIJKL-999, ABC-1

Improved JIRA ID Regex (JavaScript):

I also needed this regex in JavaScript. Unfortunately, JavaScript does not support the LookBehind (?<!a)b, and so I had to port it to LookAhead a(?!b) and reverse everything:

var jira_matcher = /\d+-[A-Z]+(?!-?[a-zA-Z]{1,10})/g

This means the string to be matched needs to be reversed ahead of time, too:

 

var s = "BF-18 abc-123 X-88 ABCDEFGHIJKL-999 abc XY-Z-333 abcDEF-33 ABC-1"
s = reverse(s)
var m = s.match(jira_matcher);

// Also need to reverse all the results!
for (var i = 0; i < m.length; i++) {
m[i] = reverse(m[i])
}
m.reverse()
console.log(m)

// Output: [ 'BF-18', 'X-88', 'ABCDEFGHIJKL-999', 'ABC-1' ]

 

 

Shaun Dunning February 12, 2018
var jiraMatcher = /((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-\d+)/g;

For javascript, this works without having to do the crazy reversal stuff and accounts for the fact that numbers can be used in the Jira project key prefix.

Like # people like this
JC May 31, 2019

this does not work. The answer provided by @G__Sylvie_Davies__bit-booster_com_ with the reversal does work. 

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 10, 2020

Slightly adjusted @Shaun Dunning version to do not match "KEY-0":

/^((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-[1-9][0-9]*)$/g
Like Sergey Protko likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events