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

Mar*_*iek 2 php regex

我有一个正则表达式,最终会有点长,它会让它更容易阅读,让它跨越多行.

我试过这个,但它只是barfs.

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
);
Run Code Online (Sandbox Code Playgroud)

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

Kon*_*lph 5

您可以使用扩展语法:

preg_match("/
    test
/x", $foo, $bar);
Run Code Online (Sandbox Code Playgroud)