Wondering if anyone out there can shed some light on why the following regular expression is failing when used in PHP's preg_match function:-
<?php
$str = '\tmp\phpDC1C.tmp';
preg_match('|\\tmp\\([A-Za-z0-9]+)|', $str, $matches);
print_r($matches);
?>
This results in the error message "preg_match(): Compilation failed: unmatched parentheses" despite the fact that the pattern appears to be valid. I've tested it with an online PHP Regular Expression tester and the Linux tool Kiki. Seems like PHP is escaping the opening parenthesis rather than the backslash.
I've got round the issue by using str_replace to swap the backslashes for forward ones. This works for my situation but it would be nice to know why this regular expression is failing.
To encode a literal backslash, you need to escape it twice: Once for the string, and once for the regex engine:
preg_match('|\\\\tmp\\\\([A-Za-z0-9]+)|', $str, $matches);
In PHP (when using single-quoted strings), this is only relevant for actual backslashes; other regex escapes are OK with a single backslash:
preg_match('/\bhello\b/', $subject)
This is covered in the manual (see the box labeled "Note:" at the top of the page).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With