有谁知道为什么我的preg_match RegEx没有返回结果?

时间:2010-09-11 22:34:35

标签: php

我刚刚在another thread帮助了一个已经过验证的正则表达式。我可以看到它实际上正在使用Rubular但是当我将正则表达式插入preg_match时,我什么都没有。

这是我的preg_match函数的正则表达式:

    preg_match('/^!!([0-9]{5}) +.*? +[MF] ([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3}) + ([A-Z])[0-9]{3}([0-9]{4})([0-9]{2})([0-9]{2})/', $res, $matches);

我得到的只是一个空数组返回。

1 个答案:

答案 0 :(得分:1)

问题是你在正则表达式中添加了两个额外的空格,这些空格不应该存在并导致匹配失败。

/^!!([0-9]{5}) +.*? +[MF] ([0-9]{3})([0-9]{3})([A-Z]{3})([A-Z]{3}) + ([A-Z])...
                         ^                                          ^
                       here                                       and here

正则表达式中的空格很重要(默认情况下)。正则表达式中的空格与目标字符串中的空格匹配。删除这两个空格可以解决问题。

ideone上查看它(这次是一个PHP示例)。

array(10) {
  [0]=>
  string(39) "!!92519 C 01 M600200BLNBRN D55420090205"
  [1]=>
  string(5) "92519"
  [2]=>
  string(3) "600"
  [3]=>
  string(3) "200"
  [4]=>
  string(3) "BLN"
  [5]=>
  string(3) "BRN"
  [6]=>
  string(1) "D"
  [7]=>
  string(4) "2009"
  [8]=>
  string(2) "02"
  [9]=>
  string(2) "05"
}