Latest recipes tagged "replace"http://code.activestate.com/recipes/tags/replace/new/2016-04-28T13:24:15-07:00ActiveState Code RecipesRecursive find replace in files using regex (Python)
2016-04-28T13:24:15-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/580653-recursive-find-replace-in-files-using-regex/
<p style="color: grey">
Python
recipe 580653
by <a href="/recipes/users/4170754/">ccpizza</a>
(<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/search/">search</a>).
Revision 5.
</p>
<h4 id="recursively-find-and-replace-text-in-files-under-a-specific-folder-with-preview-of-changed-data-in-dry-run-mode">Recursively find and replace text in files under a specific folder with preview of changed data in dry-run mode</h4>
<h5 id="example-usage">Example Usage</h5>
<p><strong>See what is going to change (dry run):</strong></p>
<pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --dryrun
</code></pre>
<p><strong>Do actual replacement:</strong></p>
<pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12"
</code></pre>
<p><strong>Do actual replacement and create backup files:</strong></p>
<pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --create-backup
</code></pre>
<p><strong>Same action as previous command with short-hand syntax:</strong></p>
<pre class="prettyprint"><code>find_replace.py -d project/myfolder -s "\d{4}-\d{2}-\d{2}" -r "2012-12-12" -b
</code></pre>
<p>Output of <code>find_replace.py -h</code>:</p>
<pre class="prettyprint"><code>DESCRIPTION:
Find and replace recursively from the given folder using regular expressions
optional arguments:
-h, --help show this help message and exit
--dir DIR, -d DIR folder to search in; by default current folder
--search-regex SEARCH_REGEX, -s SEARCH_REGEX
search regex
--replace-regex REPLACE_REGEX, -r REPLACE_REGEX
replacement regex
--glob GLOB, -g GLOB glob pattern, i.e. *.html
--dryrun, -dr don't replace anything just show what is going to be
done
--create-backup, -b Create backup files
USAGE:
find_replace.py -d [my_folder] -s <search_regex> -r <replace_regex> -g [glob_pattern]
</code></pre>
generates a set of binary files by doing ASCII replacements on a master binary file, controlled by a csv file (Python)
2015-09-16T16:25:49-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/579099-generates-a-set-of-binary-files-by-doing-ascii-rep/
<p style="color: grey">
Python
recipe 579099
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/replace/">replace</a>).
</p>
<p>A hack that saved me a lot of time. Well perhaps not so much, but it was funnier than to do it manually. </p>
Recursive 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 ".", "<span[^>]*>", "<div>", "*.html" backup
</code></pre>
<p><strong>Note</strong>:
On win32 the <code><</code> and <code>></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>
Context manager to atomically replace a file (Python)
2012-06-17T12:09:49-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578166-context-manager-to-atomically-replace-a-file/
<p style="color: grey">
Python
recipe 578166
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/atomic/">atomic</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/replace/">replace</a>).
Revision 3.
</p>
<p>This context manager ensures that a file's content is either replaced atomically with new contents or left unchanged. An exception or other error will not leave it empty or with partial content.</p>
For Characters in a String, Replace with Character (Python)
2012-03-23T20:44:23-07:00Andrew Yurisichhttp://code.activestate.com/recipes/users/4180867/http://code.activestate.com/recipes/578086-for-characters-in-a-string-replace-with-character/
<p style="color: grey">
Python
recipe 578086
by <a href="/recipes/users/4180867/">Andrew Yurisich</a>
(<a href="/recipes/tags/character/">character</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/substitute/">substitute</a>, <a href="/recipes/tags/text/">text</a>).
Revision 9.
</p>
<p>Useful if you need to replace many different characters with all of the same character. Can accept an unlimited number of request to replace.</p>
<p>Does not work with words, only characters. You can, however, replace single characters with words. I may go back and re-implement it using tuples for the keys, but this would make searching the text for any matches pretty expensive, I'd imagine. At that point, it's mostly a job for a regex, and those tools already exist.</p>
C# String.Replace with a char array (Java)
2010-12-24T09:58:35-08:00John Hurlimanhttp://code.activestate.com/recipes/users/4174599/http://code.activestate.com/recipes/577516-c-stringreplace-with-a-char-array/
<p style="color: grey">
Java
recipe 577516
by <a href="/recipes/users/4174599/">John Hurliman</a>
(<a href="/recipes/tags/csharp/">csharp</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Takes a string and replaces all characters matching a given array with a given string</p>
Improved range function (Python)
2010-06-07T01:50:20-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577256-improved-range-function/
<p style="color: grey">
Python
recipe 577256
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/replace/">replace</a>).
Revision 2.
</p>
<p>While using the built-in range function a while ago. I found an odd (perhaps bug) where the range function couldn't use float steps. I am not sure if that was intended for simplicity or not, but I wrote my own range function that goes beyond that anyway.</p>
lreplace() and rreplace(): Replace the beginning and ends of a strings (Python)
2010-06-04T02:18:48-07:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/577252-lreplace-and-rreplace-replace-the-beginning-and-en/
<p style="color: grey">
Python
recipe 577252
by <a href="/recipes/users/4169722/">Dan McDougall</a>
(<a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Python newbies will often make the following mistake (I certainly have =):</p>
<pre class="prettyprint"><code>>>> test = """this is a test:
... tis the season for mistakes."""
>>> for line in test.split('\n'):
... print line.lstrip('this')
...
is a test
the season for mistakes.
</code></pre>
<p>The mistake is assuming that lstrip() (or rstrip()) strips a string (whole) when it actually strips all of the provided characters in the given string. Python actually comes with no function to strip a string from the left-hand or right-hand side of a string so I wrote this (very simple) recipe to solve that problem. Here's the usage:</p>
<pre class="prettyprint"><code>>>> test = """this is a test:
... tis the season for mistakes."""
>>> for line in test.split('\n'):
... print lreplace('this', '', line)
...
is a test
tis the season for mistakes.
</code></pre>
<p>I really wish Python had these functions built into the string object. I think it would be a useful addition to the standard library. It would also be nicer to type this:</p>
<pre class="prettyprint"><code>line.lreplace('this', '')
</code></pre>
<p>Instead of this:</p>
<pre class="prettyprint"><code>lreplace('this','',line)
</code></pre>
REAL case insensitive string replace (Python)
2009-04-25T15:48:50-07:00Jonas Haaghttp://code.activestate.com/recipes/users/4169206/http://code.activestate.com/recipes/576715-real-case-insensitive-string-replace/
<p style="color: grey">
Python
recipe 576715
by <a href="/recipes/users/4169206/">Jonas Haag</a>
(<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/case_insensitive/">case_insensitive</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/str/">str</a>).
Revision 2.
</p>
<p>REAL case insensitive version of <code>str.replace</code> that keeps the letter case of the original expression (Doesn't only replace <code>Foo</code> and <code>foo</code> to <code>...foo...</code> but to <code>...Foo..</code> and <code>...foo...</code>).</p>