Search code examples
phpregexpcre

Regex for matching single-quoted strings fails with PHP


So I have this regex:

/'((?:[^\\']|\\.)*)'/

It is supposed to match single-quoted strings while ignoring internal, escaped single quotes \'

It works here, but when executed with PHP, I get different results. Why is that?


Solution

  • This might be easier using negative lookbehind. Note also that you need to escape the slashes twice - once to tell PHP that you want a literal backslash, and then again to tell the regex engine that you want a literal backslash.

    Note also that your capturing expression (.*) is greedy - it will capture everything between ' characters, including other ' characters, whether they are escaped or not. If you want it to stop after the first unescaped ', use .*? instead. I have used the non-greedy version in my example below.

    <?php
    
    $test = "This is a 'test \' string' for regex selection";
    $pattern = "/(?<!\\\\)'(.*?)(?<!\\\\)'/";
    
    echo "Test data: $test\n";
    echo "Pattern:   $pattern\n";
    
    if (preg_match($pattern, $test, $matches)) {
        echo "Matches:\n";
        var_dump($matches);
    }