Search code examples
javaregexquotes

Regex matching quoted string but ignoring escaped quotation mark


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...


Solution

  • Try this one:

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

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

    Online Demo