Note that there are some explanatory texts on larger screens.

plurals
  1. POPython/Regex - Expansion of Parentheses and Slashes
    primarykey
    data
    text
    <p>I'm looking for a way to expand numbers that are separated by slashes. In addition to the slashes, parentheses, may be used around some (or all) numbers to indicate a "group" which may be repeated (by the number of times directly following the parentheses) or repeated in reverse (followed by 's' as shown in the second set of examples). Some examples are:</p> <pre><code>1 -&gt; ['1'] -&gt; No slashes, no parentheses 1/2/3/4 -&gt; ['1', '2', '3', '4'] -&gt; basic example with slashes 1/(2)4/3 -&gt; ['1', '2', '2', '2', '2', '3'] -&gt; 2 gets repeated 4 times 1/(2/3)2/4 -&gt; ['1', '2', '3', '2', '3', '4'] -&gt; 2/3 is repeated 2 times (1/2/3)2 -&gt; ['1', '2', '3', '1', '2', '3'] -&gt; Entire sequence is repeated twice (1/2/3)s -&gt; ['1', '2', '3', '3', '2', '1'] -&gt; Entire sequence is repeated in reverse 1/(2/3)s/4 -&gt; ['1', '2', '3', '3', '2', '4'] -&gt; 2/3 is repeated in reverse </code></pre> <p>In the most general case, there could even be nested parentheses, which I know generally make the use of regex impossible. In the current set of data I need to process, there are no nested sets like this, but I could see potential use for it in the future. For example:</p> <pre><code>1/(2/(3)2/4)s/5 -&gt; 1/(2/3/3/4)s/5 -&gt; 1/2/3/3/4/4/3/3/2/5 -&gt; ['1', '2', '3', '3', '4', '4', '3', '3', '2', '5'] </code></pre> <p>I know of course that regex cannot do all of this (especially with the repeating/reversing sets of parenthesis). But if I can get a regex that at least separates the strings of parenthesis from those not in parenthesis, then I could probably make some loop pretty easily to take care of the rest. So, the regex I'd be looking for would do something like:</p> <pre><code>1 -&gt; ['1'] 1/2/3/4 -&gt; ['1', '2', '3', '4'] 1/(2)4/3 -&gt; ['1', '(2)4', '3'] 1/(2/3)2/4 -&gt; ['1', '(2/3)2', '4'] 1/(2/(3)2/4)s/5 -&gt; ['1', '(2/(3)/2/4)s', '5'] </code></pre> <p>And then I could loop on this result and continue expanding any parentheses until I have only digits.</p> <p><strong>EDIT</strong></p> <p>I wasn't totally clear in my original post. In my attempt to make the examples as simple as possible, I perhaps oversimplified them. This needs to work for numbers >= 10 as well as negative numbers.</p> <p>For example:</p> <pre><code>1/(15/-23)s/4 -&gt; ['1', '(15/-23)s', '4'] -&gt; ['1', '15', '-23', '-23', '15', '4'] </code></pre>
    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