简体   繁体   中英

Remove double quotes from line

I have a string that looks like this

"He said, ""What?"""

In the entire file, there's actually more lines like that, separated by commas. The output of that line should look something like this:

He said, "What?!!"

I'm trying to do that by using this method:

Pattern pattern = Pattern.compile("\\s*(\"[^\"]*\"|[^,]*)\\s*");
            Matcher matcher = pattern.matcher(line);
            while (matcher.find()) 
            {
                System.out.println(matcher.group(1));
                lines.add(matcher.group(1)); //adds each line to an arraylist
            }

However, the output I'm getting is this:

He said,
What?

I'm pretty sure the cause is with my regular expressions since all this does is remove all the double quotes.

为什么不使用String#replaceAll

line.replaceAll("\"", "");

It's because your regular expression matches

"He said, "

then

"What?"

then

""

It seems like what you actually want is to remove one level of double-quotes. To do that, you need to use lookaround assertions:

Pattern pattern = Pattern.compile("\\s*\"(?!\")[^\"]*(?<!\")\"\\s*");

The process of forming quoted string is:

  1. Escape (double) the double quotes in the string
  2. Surround the resulting string with double quotes

The code below just reverses this process:

It first removes the outer double quotes, then un-escapes the inner double quotes, and then splits:

public static void main(String[] args) {
    String input = "\"He said, \"\"What?\"\"\"";
    String[] out = input.replaceAll("^(\")|(\")$", "").replace("\"\"", "\"").split(", ");
    for (String o : out) {
        System.out.println(o);
    }
}

Output:

He said
"What?"

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