2

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.

vin
  • 231
  • 1
  • 17
shrikant5
  • 25
  • 4

1 Answers1

0

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.

Alkano
  • 116
  • 3