Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular Expression to split on specific character ONLY if that character is not in a pair
    text
    copied!<p>After finding the fastest string replace algorithm in <a href="https://stackoverflow.com/questions/1919096/mass-string-replace-in-python">this thread</a>, I've been trying to modify one of them to suit my needs, particularly <a href="https://stackoverflow.com/questions/1919096/mass-string-replace-in-python/1919221#1919221">this one</a> by gnibbler.</p> <p>I will explain the problem again here, and what issue I am having.</p> <p>Say I have a string that looks like this:</p> <pre><code>str = "The &amp;yquick &amp;cbrown &amp;bfox &amp;Yjumps over the &amp;ulazy dog" </code></pre> <p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&amp;y" and "&amp;c"). I need to replace these characters with an appropriate value that I have in a dictionary, like so:</p> <pre><code>dict = {"y":"\033[0;30m", "c":"\033[0;31m", "b":"\033[0;32m", "Y":"\033[0;33m", "u":"\033[0;34m"} </code></pre> <p>Using gnibblers solution provided in my previous thread, I have this as my current solution:</p> <pre><code>myparts = tmp.split('&amp;') myparts[1:]=[dict.get(x[0],"&amp;"+x[0])+x[1:] for x in myparts[1:]] result = "".join(myparts) </code></pre> <p>This works for replacing the characters properly, and does not fail on characters that are not found. The only problem with this is that there is no simple way to <strong>actually</strong> keep an ampersand in the output. The easiest way I could think of would be to change my dictionary to contain:</p> <pre><code>dict = {"y":"\033[0;30m", "c":"\033[0;31m", "b":"\033[0;32m", "Y":"\033[0;33m", "u":"\033[0;34m", "&amp;":"&amp;"} </code></pre> <p>And change my "split" call to do a regex split on ampersands that are NOT followed by other ampersands.</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; tmp = "&amp;yI &amp;creally &amp;blove A &amp;&amp; W &amp;uRootbeer." &gt;&gt;&gt; tmp.split('&amp;') ['', 'yI ', 'creally ', 'blove A ', '', ' W ', 'uRootbeer.'] &gt;&gt;&gt; re.split('MyRegex', tmp) ['', 'yI ', 'creally ', 'blove A ', '&amp;W ', 'uRootbeer.'] </code></pre> <p>Basically, I need a Regex that will split on the <strong>first ampersand of a pair</strong>, and every <strong>single</strong> ampersand, to allow me to escape it via my dictionary.</p> <p>If anyone has any better solutions please feel free to let me know.</p>
 

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