我想我一直在看这个太久了。 我似乎无法让这个正则表达式工作。
代码:
$pattern='/([0-9A-Z\-])*(#)(\s*)/i';
if (preg_match($pattern,'B-25-1abc-SW-19# ',$matches) ) {
echo $matches[0];
}
(来自评论):
我期待它打印出B-25-1abc-SW-19#
文本,但它不打印任何东西,因为它没有进入if语句的真正部分。
我也尝试将模式更改为:
$pattern='/^$([0-9A-Z\-])*(#)(\s*)/i';
但这也没有解决。
答案 0 :(得分:1)
你可能放错了你的量词(虽然它应该仍然匹配整个字符串):
$pattern='/([0-9A-Z\-])*(#)(\s*)/i';
应该是:
$pattern='/([0-9A-Z\-]*)(#)(\s*)/i';
如果你真的只是matches[0]
,你应该检查原来的字符串,也许你的-
有一些utf8字符,而不是你模式中的负字符(或者相反)。
答案 1 :(得分:1)
我也是在phpfiddle上尝试过它,它确实将结果返回给我。我还为你的正则表达式做了一些编辑。
http://phpfiddle.org/main/code/3x6-6w2
也许您正在处理正在处理的字符串问题?
答案 2 :(得分:0)
明星是在第一次之后而不是]:)
$pattern='/([0-9A-Z\-]*)(#)(\s*)/i';
$matches= array();
if (preg_match($pattern,'B-25-1abc-SW-19# ',$matches) ) {
print_r($matches);
}
//N.B.
//http://php.net/manual/en/function.preg-match.php
//If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.