Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use the regular expression functions from the <a href="http://docs.python.org/library/re.html" rel="nofollow noreferrer">re</a> module. For example like this:</p> <pre><code>import re pattern = re.compile(r"^Name=(.*)$", flags=re.MULTILINE) re.sub(pattern, "Name=%s" % replace_name, s) </code></pre> <p>(The <code>re.MULTILINE</code> option makes <code>^</code> and <code>$</code> match the beginning and the end of a line, respectively, in addition to the beginning and the end of the string.)</p> <hr> <p><strong>Edited to add:</strong> Based on your comments to <a href="https://stackoverflow.com/questions/4388365/replacing-variable-text-in-between-two-known-elements/4388401#4388401">Emil's answer</a>, it seems you are manipulating <a href="http://standards.freedesktop.org/desktop-entry-spec/latest/" rel="nofollow noreferrer">Desktop Entry</a> files. Their syntax seems to be quite close to that used by the <a href="http://docs.python.org/library/configparser.html" rel="nofollow noreferrer">ConfigParser</a> module (perhaps some differences in the case-sensitivity of section names, and the expectation that comments should be preserved across a parse/serialize cycle).</p> <p>An example:</p> <pre><code>import ConfigParser parser = ConfigParser.RawConfigParser() parser.optionxform = str # make option names case sensitive parser.read("/etc/skel/examples.desktop") parser.set("Desktop Entry", "Name", replace_name) parser.write(open("modified.desktop", "w")) </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.
 

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