php - Regex involving nested delimiters/quotes -
i have string enclosed either apostrophes or double-quotes. within string, other ('non-enclosing') character may appear. i'd extract contents of string using regex.
example: string = "isn't"; , want extract isn't.
using /[\'"]([^\'"]*)[\'"]/ doesn't work because doesn't impose constraint string opened , closed same character.
using /([\'"])([^\'"]*)(?1)/ fixes that, disallows 'other' character occurring within string. need /([\'"])(!(?1)*)(?1)/ how write that?
as bonus, can avoid capturing opening character ?1 contains string contents?
group index 1 contains characters present within double quotes or single quotes.
(?|"([^"]*)"|'([^']*)') or
you use below regex also,
([\'"])((?:(?!\1).)++)\1 pattern explanation:
([\'"])captures starting single or double quotes.((?:(?!\1).)+)captures 1 or more characters not of character present inside group index 1.\1must end character captured group 1.
Comments
Post a Comment