【发布时间】:2016-08-02 05:54:47
【问题描述】:
我需要这个正则表达式的帮助来仅捕获字符串中的完全匹配并将其放入变量中
我只想推断这些值(固定列表;没有其他数字):
004010H222A1 or
004010H223A2 or
004010H220A1 or
004010H279A1 or
004010H279A1 or
004010H217
从给定的字符串
示例:
$str = "this is the code 004010H222A1 the rest is irrelevant";
$str = "the random number is 004010H223A2 ** anything else is irrelevant";
$str = "the last lottery number 004010H220A1 ~~ the rest is irrelevant";
$str = "yet another random sentence 004010H279A1 the rest is irrelevant";
$str = "any sentence before what i want 004010H279A1 the rest is irrelevant";
$str = "last winning number 004010H217~~~";
if ($str =~ /\b(004010H[2][1|2|7][0|2|3|7|9])(A[1|2])?\b/){
print "found exact match\n";
##put result into a variable
##example:
## $exact_match = <found eg 004010H222A1>;
##print $exact_match;
}
我怎样才能将我想要的内容精确匹配到一个变量中然后显示它?也许我就是只见树木不见森林。提前感谢您的帮助
【问题讨论】:
-
^.*(004010H[0-9A]{0,10})
-
或者这个只是那个集合:^.*(004010H222A1|004010H223A2|004010H220A1|004010H279A1|004010H279A1|004010H217)