Question

I'm trying to use re.findall(pattern, string) to pull out a Jira Key from a Git Log line. My example input would be something like:

58df2ac Merge remote-tracking branch 'origin/ABC-1234' into release-1.1.0
df40f59 Merge branch 'ABC-2345' into release-1.1.1

And what I want to get out of this is just the ABC-1234 & ABC-2345.

I know the logic I'd want to use would be starts with ABC- and goes to until it finds a non number like a quote, space or letter.

Any help in figuring the correct regex would be appreciated.

Thanks

Was it helpful?

Solution

This will work:

 re.findall('ABC-[0-9]+', string)

[0-9] specifies any Arabic numeral. It is preferable to \d because the latter's behavior depends on the active locale. The + specifies that the previous pattern must be matched one or more times.

OTHER TIPS

This works:

import re

st='''\
58df2ac Merge remote-tracking branch 'origin/ABC-1234' into release-1.1.0
df40f59 Merge branch 'ABC-2345' into release-1.1.1'''

for line in st.splitlines():
    print re.findall(r"'.*ABC-(.*)'",line)

Or, if you know that 'ABC-XXX' is only digits, this is better:

    print re.findall(r"'.*ABC-(\d+)",line)

Either case, prints:

['1234']
['2345']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top