<p>I have a regex that is going to end up being a bit long and it'd make it much easier to read to have it across multiple lines.</p>

<p>I tried this but it just barfs.</p>

<pre><code>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-&gt;getResultVar('FullMessage'),
    $atmp
);
</code></pre>

<p>Is there are way to pass a regex in the above form to preg_match?</p>

[{'answer_id': 22572, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': True, 'text': '<p>You can use the extended syntax:</p>\n\n<pre><code>preg_match("/\n    test\n/x", $foo, $bar);\n</code></pre>\n'}, {'answer_id': 22574, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 1, 'selected': False, 'text': '<p>OK, here\'s a solution:</p>\n\n<pre><code>preg_match(\n                \'/(?x)^J[0-9]{7}:\\s+\n                (.*?)             #Extract the Transaction Start Date msg\n                \\s+J[0-9]{7}:\\s+Project\\sname:\\s+\n                (.*?)             #Extract the Project Name\n                \\s+J[0-9]{7}:\\s+Job\\sname:\\s+\n                (.*?)             #Extract the Job Name\n                \\s+J[0-9]{7}:\\s+/\'\n                , $this-&gt;getResultVar(\'FullMessage\'), $atmp);\n</code></pre>\n\n<p>The key is <strong>(?x)</strong> at the beginning which makes whitespace insignificant and allows comments.</p>\n\n<p>It\'s also important that there\'s no whitespace between the starting and ending quotes and the start &amp; end of the regex.</p>\n\n<p>My first attempt like this gave errors:</p>\n\n<pre><code>preg_match(\'\n                /(?x)^J[0-9]{7}:\\s+\n                (.*?)             #Extract the Transaction Start Date msg\n                \\s+J[0-9]{7}:\\s+Project\\sname:\\s+\n                (.*?)             #Extract the Project Name\n                \\s+J[0-9]{7}:\\s+Job\\sname:\\s+\n                (.*?)             #Extract the Job Name\n                \\s+J[0-9]{7}:\\s+/\n           \', $this-&gt;getResultVar(\'FullMessage\'), $atmp);\n</code></pre>\n\n<p>What <a href="https://stackoverflow.com/questions/22552/passing-a-commented-multi-line-freespace-regex-to-pregmatch#22572">Konrad said</a> also works and feels a little easier than sticking <strong>(?x)</strong> at the beginning.</p>\n'}, {'answer_id': 22580, 'author': 'Huppie', 'author_id': 1830, 'author_profile': 'https://Stackoverflow.com/users/1830', 'pm_score': 0, 'selected': False, 'text': '<p>In PHP the comment syntax looks like this:<pre>(?# Your comment here)</pre></p>\n<pre><code>preg_match(\'\n            ^J[0-9]{7}:\\s+\n            (.*?)             (?#Extract the Transaction Start Date msg)\n            \\s+J[0-9]{7}:\\s+Project\\sname:\\s+\n            (.*?)             (?#Extract the Project Name)\n                \\s+J[0-9]{7}:\\s+Job\\sname:\\s+\n            (.*?)             (?#Extract the Job Name)\n            \\s+J[0-9]{7}:\\s+\n            \', $this-&gt;getResultVar(\'FullMessage\'), $atmp);\n</code></pre>\n<p>For more information see the <a href="http://nl.php.net/manual/en/regexp.reference.php" rel="nofollow noreferrer">PHP Regular Expression Syntax Reference</a></p>\n<p>You can also use the PCRE_EXTENDED (or \'x\') <a href="http://nl.php.net/manual/en/reference.pcre.pattern.modifiers.php" rel="nofollow noreferrer">Pattern Modifier</a> as Mark shows in his example.</p>\n'}, {'answer_id': 22582, 'author': 'rix0rrr', 'author_id': 2474, 'author_profile': 'https://Stackoverflow.com/users/2474', 'pm_score': 1, 'selected': False, 'text': "<ul>\n<li>You should add delimiters: the first character of the regex will be used to indicate the end of the pattern.</li>\n<li>You should add the 'x' flag. This has the same result as putting (?x) at the beginning, but it is more readable imho.</li>\n</ul>\n"}, {'answer_id': 22589, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, you can add the <code>/x</code> <a href="http://us.php.net/manual/en/reference.pcre.pattern.modifiers.php" rel="nofollow noreferrer">Pattern Modifier</a>.</p>\n\n<blockquote>\n  <p>This modifier turns on additional\n  functionality of PCRE that is\n  incompatible with Perl. Any backslash\n  in a pattern that is followed by a\n  letter that has no special meaning\n  causes an error, thus reserving these\n  combinations for future expansion. By\n  default, as in Perl, a backslash\n  followed by a letter with no special\n  meaning is treated as a literal. There\n  are at present no other features\n  controlled by this modifier.</p>\n</blockquote>\n\n<p>For your example try this:</p>\n\n<pre><code>preg_match(\'/\n              ^J[0-9]{7}:\\s+\n              (.*?)             #Extract the Transaction Start Date msg\n              \\s+J[0-9]{7}:\\s+Project\\sname:\\s+\n              (.*?)             #Extract the Project Name\n              \\s+J[0-9]{7}:\\s+Job\\sname:\\s+\n              (.*?)             #Extract the Job Name\n              \\s+J[0-9]{7}:\\s+\n            /x\', $this-&gt;getResultVar(\'FullMessage\'), $atmp);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/22552', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/305/']