Java Regex for matching string with quotes

I know that there are already many questions like mine, but I have not found an answer that works in Java. Therefore, I am writing a new question.

I have text files with content like this:

 key1 = "This is a \" test \ "text with escapes using '\\' characters";
 key2 = 'It must work with \' single \ 'quotes and "double" quotes';

I need a regular expression that matches the values ​​in double quotes (or single quotes). This regex must support hidden quotes and flush backslashes. The regular expression should work with standard Java Pattern / Matcher patterns.

+7
java regex escaping quotes
source share
1 answer

Try this regex:

'([^\\']+|\\([btnfr"'\\]|[0-3]?[0-7]{1,2}|u[0-9a-fA-F]{4}))*'|"([^\\"]+|\\([btnfr"'\\]|[0-3]?[0-7]{1,2}|u[0-9a-fA-F]{4}))*" 

And as a string literal:

 "'([^\\\\']+|\\\\([btnfr\"'\\\\]|[0-3]?[0-7]{1,2}|u[0-9a-fA-F]{4}))*'|\"([^\\\\\"]+|\\\\([btnfr\"'\\\\]|[0-3]?[0-7]{1,2}|u[0-9a-fA-F]{4}))*\"" 
+8
source share

All Articles