将注释的多行(自由空间)正则表达式传递给preg_match

时间:2020-03-05 18:42:03  来源:igfitidea点击:

我有一个正则表达式将最终有点长,它可以使跨多行阅读变得更容易。

我尝试了这个,但它只是啤酒。

preg_match('
                ^J[0-9]{7}:\s+
                (.*?)             #Extract the Transaction Start Date msg
                \s+J[0-9]{7}:\s+Project\sname:\s+
                (.*?)             #Extract the Project Name
                    \s+J[0-9]{7}:\s+Job\sname:\s+
                (.*?)             #Extract the Job Name
                \s+J[0-9]{7}:\s+
                ', $this->getResultVar('FullMessage'), $atmp);

有没有办法将上述形式的正则表达式传递给preg_match?

解决方案

回答

我们可以使用扩展语法:

preg_match("/
    test
/x", $foo, $bar);

回答

好的,这是一个解决方案:

preg_match(
                '/(?x)^J[0-9]{7}:\s+
                (.*?)             #Extract the Transaction Start Date msg
                \s+J[0-9]{7}:\s+Project\sname:\s+
                (.*?)             #Extract the Project Name
                \s+J[0-9]{7}:\s+Job\sname:\s+
                (.*?)             #Extract the Job Name
                \s+J[0-9]{7}:\s+/'
                , $this->getResultVar('FullMessage'), $atmp);

关键字的开头是(?x),这会使空格变得微不足道并允许注释。

同样重要的是,在引号和结束引号之间以及正则表达式的开始和结束之间不能有空格。

我这样的第一次尝试给出了错误:

preg_match('
                /(?x)^J[0-9]{7}:\s+
                (.*?)             #Extract the Transaction Start Date msg
                \s+J[0-9]{7}:\s+Project\sname:\s+
                (.*?)             #Extract the Project Name
                \s+J[0-9]{7}:\s+Job\sname:\s+
                (.*?)             #Extract the Job Name
                \s+J[0-9]{7}:\s+/
           ', $this->getResultVar('FullMessage'), $atmp);

Konrad所说的内容也很有效,并且比起初粘贴(?x)容易一些。

回答

在PHP中,注释语法如下所示:

(?# Your comment here)
preg_match('
            ^J[0-9]{7}:\s+
            (.*?)             (?#Extract the Transaction Start Date msg)
            \s+J[0-9]{7}:\s+Project\sname:\s+
            (.*?)             (?#Extract the Project Name)
                \s+J[0-9]{7}:\s+Job\sname:\s+
            (.*?)             (?#Extract the Job Name)
            \s+J[0-9]{7}:\s+
            ', $this->getResultVar('FullMessage'), $atmp);

有关更多信息,请参见《 PHP正则表达式语法参考》。

我们还可以使用PCRE_EXTENDED(或者" x")模式修改器,如Mark在他的示例中所示。

回答

  • 我们应该添加定界符:正则表达式的第一个字符将用于指示模式的结尾。
  • 我们应该添加" x"标志。其结果与开头(?x)相同,但恕我直言。

回答

是的,我们可以添加/ x模式修改器。

This modifier turns on additional
  functionality of PCRE that is
  incompatible with Perl. Any backslash
  in a pattern that is followed by a
  letter that has no special meaning
  causes an error, thus reserving these
  combinations for future expansion. By
  default, as in Perl, a backslash
  followed by a letter with no special
  meaning is treated as a literal. There
  are at present no other features
  controlled by this modifier.

对于示例,请尝试以下操作:

preg_match('/
              ^J[0-9]{7}:\s+
              (.*?)             #Extract the Transaction Start Date msg
              \s+J[0-9]{7}:\s+Project\sname:\s+
              (.*?)             #Extract the Project Name
              \s+J[0-9]{7}:\s+Job\sname:\s+
              (.*?)             #Extract the Job Name
              \s+J[0-9]{7}:\s+
            /x', $this->getResultVar('FullMessage'), $atmp);