Search code examples
phpregexescapingpreg-match-allsingle-quotes

Match string between single quotes including escaped single quotes


Trying to find a regular expression to handle this string using PHP preg_match_all():

include='Track Ass\'y'

The regex should be able to handle single OR double quotes, but not break on the escaped quote. Currently the regex in place looks like this:

/([^ ]*?)=["|'](.*?)(["|'])/

This breaks on the ' and only matches up to the single quote, and misses the final y.


Solution

  • It's stopping before the y because of your lazy-match ?.

    Remove the second question mark:

    (.*)