简体   繁体   中英

Java reg-ex to replace double quote inside double quote

Input string is: "outer string "inside a quote" "
and output should be: "outer string inside a quote "

Please suggest any regular expression to find the inner double quote and replace with space using Java.

You can try it this way, without regex and in one iteration :

/*
 * I assume that if after quote ther is character like "a then it is
 * beggining of cite. Rest quotes are closing ones.
 */
public static String removeInnerQuotes(String data) {

    StringBuilder sb = new StringBuilder();
    int quoteCounter = 0;
    char[] array = data.toCharArray();
    for (int i = 0; i < array.length; i++) {
        if (array[i] == '"') {
            if (i + 1 < array.length
                    && (
                            (array[i + 1] >= 'a' && array[i + 1] <= 'z') 
                        || 
                            (array[i + 1] >= 'A' && array[i + 1] <= 'Z')
                        )
                ){
                quoteCounter++;
                if (quoteCounter == 1)
                    sb.append('"');
            }
            else{
                quoteCounter--;
                if (quoteCounter == 0)
                    sb.append('"');
            }

        } else
            sb.append(array[i]);
    }
    return sb.toString();
}

public static void main(String[] args) {
    String data = "\"outer string \"inside a quote\" abc\" something outside quote, and again \"outer string \"inside a quote\" def \"";
    System.out.println(removeInnerQuotes(data));
}

output:

"outer string inside a quote abc" something outside quote, and again "outer string inside a quote def "

Assuming that there is only one level of nesting and only one inner-quote inside outer quotes, the following should work:

str.replaceAll("\"(\\b[^\"]+)?\"\\b([^\"]+)\\b\"([^\"]+\\b)?\"","\"$1$2$3\"");

It tries to detect open vs. closing quotes by word boundaries. To allow whitespaces between the outer and inner quotes (instead of a word), and even nothing in the inner quotes, use this:

str.replaceAll("\"(\\b[^\"]+|\\s+)?\"(\\b[^\"]+\\b)?\"([^\"]+\\b|\\s+)?\"","\"$1$2$3\"");

I think that for this case, a regex solution, if available might be slightly convoluted to be maintainable.

What you could do is have something like so:

String str = "outer string "inside a quote" ";
String newStr = "\"" + str.replaceAll("\"","") + "\";

The code above will remove all the quotation marks and add one at the beginning and end of the string.

EDIT:

I have come up with this, it is a bit convoluted but it does seem to work:

String str = "sdsadsaasdasdsadas\"sadsad\"the is sparta\"asdsadsa\"sdassa";
String newStr = str.replaceAll("(\".*?)\"(.*?)\"(.*?\")", "$1$2$3");
System.out.println(newStr);

It yields:

sdsadsaasdasdsadas"sadsadthe is spartaasdsadsa"sdassa

The regex above basically splits the text into 3 sections, the part prior to the first set of quotes, the set in between the inner quotations and the section after the inner quotations. It then rebuilds the string.

EDIT:

I used this code to read the string from file (note the string in this case was a 1 liner)

Scanner input = new Scanner(new File(filePath));
StringBuilder sb = new StringBuilder();
while (input.hasNextLine())
{
    sb.append(input.nextLine().trim()).append(" ");
}
input.close();
String str = sb.toString().trim();
String newStr = str.replaceAll("(\".*?)\"(.*?)\"(.*?\")", "$1$2$3");
System.out.println(newStr);

It yielded:

"outer string inside a quote "

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