Popular recipes tagged "replace" but not "files"http://code.activestate.com/recipes/tags/replace-files/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 ".", "<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>
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>
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>
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>