我正在使用正则表达式来获取包含引号的字符串值。在下面的示例中,我想获取值摘要键为“Here is“summary”。目前我只得到“Here is ”作为下面程序的输出。我想转义第一个和最后一个双引号之间的所有双引号.
String in = "summary = \"Here is \"summary\"";
Pattern p = Pattern.compile("'(.*?)'|\"(.*?)[^\\\"]+\"");
Matcher m = p.matcher(in);
while(m.find()) {
System.out.println(m.group());
}
感谢您的帮助。
最佳答案
使用这个:
/\\["']((?:[^"\\]|\\.)*)\\["']/
演示:https://regex101.com/r/033EKx/1
记住:当尝试通过 "
构建正则表达式时,\
应更改为 \\
和其他特殊字符 ("
和 '
)
rStr = "/\\\\[\"\']((?:[^\"\\\\]|\\\\.)*)\\\\[\"\']/" ;
关于java - 用于获取包含引号的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42558335/