Search code examples
pythonregexperljira

Regular expression for a JIRA identifier


I'm trying to extract a JIRA identifier from a line of text.

JIRA identifiers are of the form [A-Z]+-[0-9] - I have the following pattern:

foreach my $line ( @textBlock ) {
    my ( $id ) = ( $line =~ /[\s|]?([A-Z]+-[0-9]+)[\s:|]?/ );
    push @jiraIDs, $id if ( defined $id && $id !~ /^$/ );
}

This doesn't cope if someone specifies some text which contains the pattern inside another string - for example blah_blah_ABC-123 would match upon ABC-123. I don't want to mandate that there must be a space or other delimiter in front of the match as that would fail if the identifier were at the start of the line.

Can anyone suggest the necessary runes?

Thanks.


Solution

  • You can make sure that character before your pattern is either a whitespace, or the beginning of the string using alternation. Similarly make sure, it is followed by either whitespace or end of the string.

    You can use this regex:

    my ( $id ) = ( $line =~ /(?:\s|^)([A-Z]+-[0-9]+)(?=\s|$)/ );