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.

(?|"([^"]*)"|'([^']*)') 

demo

or

you use below regex also,

([\'"])((?:(?!\1).)++)\1 

demo

pattern explanation:

  • ([\'"]) captures starting single or double quotes.
  • ((?:(?!\1).)+) captures 1 or more characters not of character present inside group index 1.
  • \1 must end character captured group 1.

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -