Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to remove unbalanced/unpartnered double quotes (in Java)
    text
    copied!<p>I thought to share this relatively smart problem with everyone here. I am trying to remove unbalanced/unpaired double-quotes from a string.</p> <p>My work is in progress, I might be close to a solution. But, I didn't get a working solution yet. <strong>I am not able to delete the unpaired/unpartnered double-quotes from the string.</strong></p> <p><strong>Example Input</strong></p> <pre><code>string1=injunct! alter ego." string2=successor "alter ego" single employer" "proceeding "citation assets" </code></pre> <p><strong>Output Should be</strong></p> <pre><code>string1=injunct! alter ego. string2=successor "alter ego" single employer proceeding "citation assets" </code></pre> <p>This problem sound similar to <a href="https://stackoverflow.com/questions/9898455/using-java-remove-unbalanced-unpartnered-paranthesis">Using Java remove unbalanced/unpartnered parenthesis</a></p> <p>Here is my code so far(it doesn't delete all the unpaird double-quotes)</p> <pre><code>private String removeUnattachedDoubleQuotes(String stringWithDoubleQuotes) { String firstPass = ""; String openingQuotePattern = "\\\"[a-z0-9\\p{Punct}]"; String closingQuotePattern = "[a-z0-9\\p{Punct}]\\\""; int doubleQuoteLevel = 0; for (int i = 0; i &lt; stringWithDoubleQuotes.length() - 3; i++) { String c = stringWithDoubleQuotes.substring(i, i + 2); if (c.matches(openingQuotePattern)) { doubleQuoteLevel++; firstPass += c; } else if (c.matches(closingQuotePattern)) { if (doubleQuoteLevel &gt; 0) { doubleQuoteLevel--; firstPass += c; } } else { firstPass += c; } } String secondPass = ""; doubleQuoteLevel = 0; for (int i = firstPass.length() - 1; i &gt;= 0; i--) { String c = stringWithDoubleQuotes.substring(i, i + 2); if (c.matches(closingQuotePattern)) { doubleQuoteLevel++; secondPass = c + secondPass; } else if (c.matches(openingQuotePattern)) { if (doubleQuoteLevel &gt; 0) { doubleQuoteLevel--; secondPass = c + secondPass; } } else { secondPass = c + secondPass; } } String result = secondPass; return result; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload