Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match " but not \"

Tags:

regex

I'm always completely at loss with regexes so I hope someone here can help.

Joomla uses this regex to validate lines of ini files:

/^(|(\[[^\]]*\])|([A-Z][A-Z0-9_\-]*\s*=(\s*(("[^"]*")|(_QQ_)))+))\s*(;.*)?$/

The lines have this format

JLIB_LOGIN_AUTHENTICATE="Username and password do not match or you do not have an account yet."

Obviously " can't be allowed in the value part but \" could be. Currently this is matched by the above regex. I can see how it's matched but I can't modify the regex not to match \". I tried adding |(\Q\"\E)to various places but no luck.

like image 248
Rouven Weßling Avatar asked Jan 17 '23 00:01

Rouven Weßling


2 Answers

Change this:

"[^"]*"

To this:

"(\\"|[^"])*"

See it working online: ideone

like image 193
Mark Byers Avatar answered Jan 22 '23 00:01

Mark Byers


replace [^"] with ([^"]|(\\"))

like image 26
Karoly Horvath Avatar answered Jan 21 '23 23:01

Karoly Horvath