1

I'm trying to figure out a regular expression that will detect if there is an unescaped double quote within two double quotes. I have this, but it's not working:

if ( preg_match( '/^(\".*((?<!\\)\").*\")$/', $str ) ) {
    die("hey");
}
Brandon
  • 16,382
  • 12
  • 55
  • 88
  • I don't think it's possible to do that with a Regex. At least if you want it a bit more general - if you only need to match a single pair of double quotes, and a single unescaped one then it's possible. – Ariel Aug 02 '11 at 21:21
  • Not a regex expert so not putting this in an answer but what about `^(\\)\".*^(\\)\"` You check if there's not a \ but there is a " with anything before another " without a \ in front of it. – Alexander Varwijk Aug 02 '11 at 21:22
  • @Ariel It's for error checking. If there are any unescaped double quotes within the two doubles quotes then we have an error. That's all I need. – Brandon Aug 02 '11 at 21:29

1 Answers1

1

Try with preg_match('/([^\\]\")/', $str)

Emanuele Minotto
  • 415
  • 5
  • 10
  • 2
    This is incorrect. `\\"` is an unescaped quote preceded by a literal backslash, but it won't be matched by this regex. – bukzor Sep 06 '13 at 01:15