The RegEx you are looking for is:
/^(?:[^"]*(?:(?<=\\\)"|))*$/
Explanation: [^"]* will match the input until the first one is found " or the end of the input is reached. If " found, make sure that in (?<=\\\)" lookbehind is always preceded by / . The above script repeats recursively until the end of input is reached.
TEST: Consider the following PHP code to verify:
$arr=array('This is valid', 'This \"is Valid', 'This is al\"so Valid\"', 'This i"s invalid', 'This i"s inv"alid', 'This is a \"test', 'This is a \"test of " the emergency broadcast system - invalid'); foreach ($arr as $a) { echo "$a => "; if (preg_match('/^(?:[^"]*(?:(?<=\\\)"|))*$/', $a, $m)) echo "matched [$m[0]]\n"; else echo "didn't match\n"; }
CONCLUSION:
This is valid => matched [This is valid] This \"is Valid => matched [This \"is Valid] This is al\"so Valid\" => matched [This is al\"so Valid\"] This i"s invalid => didn't match This i"s inv"alid => didn't match This is a \"test => matched [This is a \"test] This is a \"test of " the emergency broadcast system - invalid => didn't match
source share