Regex to find content not in quotes

后端 未结 3 1388
再見小時候
再見小時候 2021-01-18 08:31

I\'m trying to find parameters outside quotes (Single ou Double)

$sql = \"
INSERT INTO Notifify (to_email, msg, date_log, from_email, ip_from)
VALUES
    (
          


        
相关标签:
3条回答
  • 2021-01-18 09:11

    You can use a negative look-behind. This way you will match exactly what's needed.

    preg_match_all('/(?<!\w):[a-z0-9_]+/', $sql, $matches);
    

    Demo

    0 讨论(0)
  • 2021-01-18 09:12
    '/^\\s*:[A-Za-z0-9_]*/m'
    

    Should do the trick, checking for beginning of line and white-space and make sure the RegEx query is set to multiline.

    edit

    preg_match_all('/(?:^\\s*)(:[A-Za-z0-9_]+)/m', $sql, $matches);
    print_r($matches[1]);
    

    This uses the passive non-capture group (?:) which puts the proper results, without the space padding into a sub array of the matches variable at index 1.

    0 讨论(0)
  • 2021-01-18 09:22

    What about this:

    /\s+:[A-Za-z0-9_]*/
    

    It's not very rigorous and might fail for more complex examples like like tennis scores (15 : 30) but is probably good enough for your needs.

    0 讨论(0)
提交回复
热议问题