简体   繁体   中英

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

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

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

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

if java you will need to protect each \\ and "

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

if the java Pattern doesn't work try with JRegex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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