Search code examples
pythonregexfindall

Python findall regex


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


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.