Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing all whitespace between brackets using regex in Python
    primarykey
    data
    text
    <p>I need to replace all unnecessary whitespace between brackets in lisp-like code like this: </p> <pre><code>(print (number-&gt;string ( (func? '+ '-) 12 13 14 15) ) ) </code></pre> <p>So that it becomes:</p> <pre><code>(display (number-&gt;string ((func? '+ '-) 12 13 14 15))) </code></pre> <p>I know it looks really simple at first sight, but it's really tricky(at least to me). Here's<br> what I've tried:</p> <pre><code>In [14]: re.sub(r"\)[ \t]*\)","))",")) ) ",0) Out[14]: ')) ) ' In [15]: re.sub(r"\)[ \t]*\)","))",") ) ",0) Out[15]: ')) ' In [16]: re.sub(r"\)[ \t]+\)","))",")) ) ",0) Out[16]: '))) ' In [17]: re.sub(r"\)[ \t]+\)","))",")) ) ",0) Out[17]: '))) ' In [18]: re.sub(r"\)[ \t]+\)","))",") ) ) ",0) Out[18]: ')) ) ' In [19]: re.sub(r"\)[ \t]+\)","))",") ) ) ",1) Out[19]: ')) ) ' In [20]: re.sub(r"\)[ \t]+\)","))",") ) ) ",0) Out[20]: ')) ) ' In [21]: re.sub(r"\)[ \t]+\)","))",") ) ) ",0,re.M) Out[21]: ')) ) ' In [22]: re.sub(r"\).+\)","))",") ) ) ",0,re.M) Out[22]: ')) ' In [23]: re.sub(r"\)\s+\)","))",") ) ) ",0,re.M) Out[23]: ')) ) ' In [24]: re.sub(r"\)([ \t]*)\)","))",") ) ",0) Out[24]: ')) ' In [25]: re.sub(r"\)([ \t]*)\)","",") ) ",0) Out[25]: ' ' In [26]: re.sub("\)([ \t]*)\)","))",") ) ",0) Out[26]: ')) ' In [27]: re.sub("\)([ \t]*)\)","))",") ) )",0) Out[27]: ')) )' In [28]: re.sub("\)([ \t]*)\)","))",") ) )",0) Out[28]: ')) )' In [29]: re.sub("\)([ \t]*?)\)","))",") ) )",0) Out[29]: ')) )' In [30]: re.sub("\)(.+?)\)","))",") ) )",0) Out[30]: ')) )' </code></pre> <p>In every case above, <code>re.sub</code> replaces the whitespace between the first brackets only and leaves the rest of the string the same way it was before. I need a regexp to replace all the whitespace.</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.
 

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