简体   繁体   English

正则表达式获取双引号之间的查找字符串

[英]Regex to get find string between double quotes

I am looking for regex to return value between the double quotes in a given string. 我正在寻找正则表达式返回给定字符串中双引号之间的值。 I am using below java code 我正在使用下面的java代码

Pattern p = Pattern.compile("\"([^\"]*)\"");

Matcher m = p.matcher(line);
int findline = 0;

while (m.find()) {
System.out.println(m.group(0));
}

The above code work fine for normal text but not for below string 上面的代码适用于普通文本,但不适用于下面的字符串

String originalString ="value = value.replaceAll(", ", ",").replaceAll(",", "\",\"").replaceAll("\\[","\""); ";

On javafile it will be like 在javafile上它将像

String originalString = "value = value.replaceAll(\", \", \",\").replaceAll(\",\", \"\\\",\\\"\").replaceAll(\"\\\\[\",\"\\\"\"); ";

Now what i am looking for is if the data between double quote contain 现在我正在寻找的是双引号之间的数据是否包含

\ or \" or \\

then ignore that rest everything it should return. 然后忽略其余所有应返回的内容。

Rest all value between double quote contain escape char so ignore that content. 其余所有双引号之间的值都包含转义字符,因此请忽略该内容。

you want to 'eat' exactly one character after the \\ it is like this in perl 您想在\\之后精确地“吃掉”一个字符,就像在perl中这样

if( $line =~ /(?:(?:[\\]?+.)|.)*?"((([\\]?+.)|.)*?)"/ ){
    print $1;
}

if java you will need to protect each \\ and " 如果是Java,则需要保护每个\\和“

Pattern.compile("(?:(?:[\\\\]?+.)|.)*?\"((([\\\\]?+.)|.)*?)\"");

if the java Pattern doesn't work try with JRegex. 如果Java模式不起作用,请尝试使用JRegex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM