Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching quoted string but ignoring escaped quotation mark

Tags:

java

regex

quotes

What I want to know is how to modify following regex: \".*?\" so it will ignore escaped " character (\") so it won't end matching at \".

For example:

parameter1 =  "      fsfsdfsd \"      "   parameter2 =   "   fsfsfs   "

I want to match:

" fsfsdfsd \" "

and

" fsfsfs "

but not

" fsfsdfsd \" " parameter2 = " fsfsfs "

etc...

like image 903
zduny Avatar asked Nov 28 '25 16:11

zduny


1 Answers

Try this one:

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

It matches "test \" though(you can probably avoid that using lookbehind). Escape the character if you need using \

Online Demo

like image 162
Sabuj Hassan Avatar answered Dec 01 '25 06:12

Sabuj Hassan