Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"preg_match(): Compilation failed: unmatched parentheses" in PHP for valid pattern

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.

like image 435
drmonkeyninja Avatar asked Apr 30 '12 13:04

drmonkeyninja


Video Answer


1 Answers

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).

like image 167
Tim Pietzcker Avatar answered Sep 20 '22 14:09

Tim Pietzcker