Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecial Character problem in regexp by python
    text
    copied!<p>I apply some regular expression on xml file to find and replace values. Normally it works.(I heard the voices saying "use xml parsers". Meanwhile I can not.) But if there is a special character in the value, it ruins everything. </p> <p>Think I have a xml file like below:</p> <pre><code>&lt;fieldset&gt; &lt;idle1&gt; &lt;value&gt;something\\n&lt;/value&gt; &lt;/idle1&gt; &lt;idle2&gt; &lt;value&gt;blabla&lt;/value&gt; &lt;/idle2&gt; &lt;/fieldset&gt; </code></pre> <p>If I try to replace value in "<code>&lt;idle2&gt;&lt;value&gt;</code>" node, value of "<code>&lt;idle1&gt;&lt;value&gt;</code>" node becomes "something\n". And when it comes to writing to file, xml becomes:</p> <pre><code> &lt;fieldset&gt; &lt;idle1&gt; &lt;value&gt;something &lt;/value&gt; &lt;/idle1&gt; &lt;idle2&gt; &lt;value&gt;blabla&lt;/value&gt; &lt;/idle2&gt; &lt;/fieldset&gt; </code></pre> <p>Well both in search and replace i use "r" string literal. But it seems not working. I solve the problem. For every search and replace, I replace "\n"s with "<code>\\n</code>" and then I write result to the file. But it is not an efficient way to use. </p> <p>Is there something I could not see? I just want to write "<code>\\n</code>" to the files. Is this so much for me to want it?</p> <p>Edit: here is my regexs':</p> <p>for search :</p> <pre><code>self.searchPattern=(&lt;fieldset&gt;)(.*?)(&lt;idle2&gt;)(.*?)(&lt;value&gt;)(.*?)(&lt;/value&gt;)(.*?)(&lt;/idle2&gt;)(.*?)(&lt;/fieldset&gt;) </code></pre> <p>for replace : </p> <pre><code>self.replacePattern=`\g&lt;1&gt;\g&lt;2&gt;\g&lt;3&gt;\g&lt;4&gt;&lt;value&gt;denemeasdasd\\\\n&lt;/value&gt;\g&lt;8&gt;\g&lt;9&gt;\g&lt;10&gt;\g&lt;11&gt;` </code></pre> <p>this is the python code for search:</p> <pre><code>self.pattern = re.compile(r''''''+self.searchPattern+'''''', flags = re.S | re.U) </code></pre> <p>and this is for replacing</p> <pre><code>outtext = self.pattern.sub(r''''''+self.replacePattern+'''''',r''''''+self.match.group(0)+'''''') </code></pre>
 

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