Popular recipes tagged "regular_expressions" but not "parsing"http://code.activestate.com/recipes/tags/regular_expressions-parsing/2013-03-21T12:33:44-07:00ActiveState Code RecipesRecursive find and replace in all files in directory with regex (Python) 2013-03-21T12:33:44-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/578312-recursive-find-and-replace-in-all-files-in-directo/ <p style="color: grey"> Python recipe 578312 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/replace/">replace</a>). </p> <p>Search and replace files recursively in a given directory. Accepts regular expressions as search and replacement patterns.</p> <p><strong>Example usage</strong>:</p> <pre class="prettyprint"><code>python find_replace_regex.py ".", "&lt;span[^&gt;]*&gt;", "&lt;div&gt;", "*.html" backup </code></pre> <p><strong>Note</strong>: On win32 the <code>&lt;</code> and <code>&gt;</code> symbols are interpreted by the shell as input/output redirection even when they are surrounded with single or double quotes, and therefore need to be escaped with the caret character <code>^</code>.</p> ActiveState recipe statistics (Python) 2011-06-02T14:52:50-07:00Kaan Ozturkhttp://code.activestate.com/recipes/users/4178179/http://code.activestate.com/recipes/577732-activestate-recipe-statistics/ <p style="color: grey"> Python recipe 577732 by <a href="/recipes/users/4178179/">Kaan Ozturk</a> (<a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/statistics/">statistics</a>, <a href="/recipes/tags/urllib2/">urllib2</a>, <a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>Downloads "All Recipe Authors" pages in ActiveState, uses regular expressions to parse author name and number of their recipes on each page. Finally, it displays the recipe submission distribution (the count of how many authors have submitted how many recipes each).</p> Formatting numbers with a state machine (implementation of a regex pattern) (Python) 2011-03-22T03:40:45-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/577618-formatting-numbers-with-a-state-machine-implementa/ <p style="color: grey"> Python recipe 577618 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>). </p> <p>I was once asked to explain how the following regular expression works which formats any integer with commas for every thousand (or group of 3 digits):</p> <pre class="prettyprint"><code>(\d)(?=(\d{3})+$) </code></pre> <p>Example:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.sub("(\d)(?=(\d{3})+$)", "\\1,", "1234") '1,234' </code></pre> <p>So here is an implementation of the above regular expression (as best as I could over a lunch break) that will hopefully highlight how a regular expression engine and finite automa work.</p> <p>Comments and feedback welcome!</p> <p>--JamesMills / prologic</p> Regular Expression for generic sequences of symbols (Python) 2009-06-13T09:51:38-07:00Emanuele Ruffaldihttp://code.activestate.com/recipes/users/4170726/http://code.activestate.com/recipes/576806-regular-expression-for-generic-sequences-of-symbol/ <p style="color: grey"> Python recipe 576806 by <a href="/recipes/users/4170726/">Emanuele Ruffaldi</a> (<a href="/recipes/tags/adt/">adt</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/sequence/">sequence</a>). Revision 2. </p> <p>Python regular expression are very powerful and efficient and they can be applied to the recognition of different types of sequences. This recipe shows how to match sequences of generic symbol set with the power of regular expression. The code uses a mapping from every entity into a character. The mapping is used both at level of sequence and in the compilation of the regular expression. When the symbol set is small it is possible to efficiently use 8 bit strings instead of full unicode.</p> Multi-Regex: Single pass replace of multiple regexes (Python) 2009-04-03T13:38:39-07:00Michael Palmerhttp://code.activestate.com/recipes/users/1827292/http://code.activestate.com/recipes/576710-multi-regex-single-pass-replace-of-multiple-regexe/ <p style="color: grey"> Python recipe 576710 by <a href="/recipes/users/1827292/">Michael Palmer</a> (<a href="/recipes/tags/regular_expressions/">regular_expressions</a>, <a href="/recipes/tags/string_substitution/">string_substitution</a>). Revision 5. </p> <p>Not really - all regexes first get combined into a single big disjunction. Then, for each match, the matching sub-regex is determined from a group name and the match object dispatched to a corresponding method, or simply replaced by a string. </p>