I have string like this:
"abcd\" efg\" hi" jklm"
I want to get sub string between two first characters "
, which is not \"
For example, in the above string, I want to get abcd\" efg\" hi
Currently, I replace \"
by another character, then use the regex "([^"]*)"
to extract the sub string between two first characters "
. Is there any way to use regex directly without replacing \"
by another character.
Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.
The backslash character (\) in a regular expression indicates that the character that follows it either is a special character (as shown in the following table), or should be interpreted literally. For more information, see Character Escapes. Escaped character. Description. Pattern.
There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.
\ The backslash suppresses the special meaning of the character it precedes, and turns it into an ordinary character. To insert a backslash into your regular expression pattern, use a double backslash ('\\').
Use this regex:
[^\\]?"(.*?[^\\])"
Explanation:
[^\\]? match an optional single character which is not backslash
"(.*? match a quote followed by anything (non-greedy)
[^\\])" match a quote preceded by anything other than backslash
This regex will match the least content between an opening quote and closing quote which does not have a backslash.
Regex101
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With