Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My first idea was too complex and didn't work. This one simplier and I hope will work. We just check if there's outer parentheses covering current pair, i.e. when we found <code>(...)</code> we check if there's open parenthesis before <code>'('</code> and closing one after <code>')'</code>. Also we need to remove them when <code>s="(.....)"</code>, i.e. when we found outermost parentheses which starts at 0 and close at the end.</p> <pre><code>private string RemoveDoubleParenthesis(string initialString) { char[] s = new char[initialString.Length]; char toRemove = '$'; Stack&lt;int&gt; stack = new Stack&lt;int&gt;(); for (int i = 0; i &lt; s.Length; i++) { s[i] = initialString[i]; if (s[i] == '(') stack.Push(i); else if (s[i] == ')') { int start = stack.Pop(); if ((start == 0 &amp;&amp; i == (s.Length - 1)) || (s[start-1] == '(' &amp;&amp; s[i+1] == ')')) { s[start] = s[i] = toRemove; } } } return new string((from c in s where c != toRemove select c).ToArray()); } </code></pre> <p>As you see, I assume that expression doesn't have errors (missing parantheses).</p> <p><strong>UPD</strong> Thanks, @retailcoder. And sorry for messing with answer versions :) </p> <pre><code>var results = string.Join("\n", new string[] { "((f1=2)AND(f2=3))", "((f1=2)AND((f2=3)))", "(((f1=2)AND(f2=3)))", "(f1=2 AND(f2=3))", "((f1=2 AND (f3=4)) AND(f2=3))" } .Select(s =&gt; string.Format("{0} -&gt; " + RemoveDoubleParenthesis(s), s))); MessageBox.Show(results); </code></pre> <p><img src="https://i.stack.imgur.com/TGzc1.png" alt="no problem, you&#39;re welcome!"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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