JIRA标识符的正则表达式 [英] Regular expression for a JIRA identifier

查看:185
本文介绍了JIRA标识符的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一行文本中提取一个JIRA标识符.

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

JIRA标识符的格式为[A-Z] +-[0-9]-我具有以下模式:

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 !~ /^$/ );
}

如果有人指定某些文本包含另一个字符串中的模式,这将无法解决-例如,blah_blah_ABC-123将与ABC-123匹配.我不想强制在比赛之前必须有一个空格或其他定界符,因为如果标识符位于行的开头,这将失败.

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?

谢谢.

推荐答案

您可以使用替换方式确保模式之前的字符是空格或字符串的开头.同样,请确保在其后跟空格或字符串结尾.

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|$)/ );

这篇关于JIRA标识符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆