python正则表达式中的特殊字符问题
我在 xml 文件上应用一些正则表达式来查找和替换值。通常它可以工作。(我听到有人说“使用 xml 解析器”。同时我不能。)但是如果值中有特殊字符,它就会毁掉一切。
认为我有一个如下所示的 xml 文件:
<fieldset>
<idle1>
<value>something\\n</value>
</idle1>
<idle2>
<value>blabla</value>
</idle2>
</fieldset>
如果我尝试替换“
<fieldset>
<idle1>
<value>something
</value>
</idle1>
<idle2>
<value>blabla</value>
</idle2>
</fieldset>
在搜索和替换中我都使用“r”字符串文字。但它似乎不起作用。我解决问题。对于每次搜索和替换,我将“\n”替换为“\\n
”,然后将结果写入文件。但这并不是一种有效的使用方式。
有什么我看不到的吗?我只想将“\\n
”写入文件。难道我就这么想要吗?
编辑:这是我的正则表达式':
用于搜索:
self.searchPattern=(<fieldset>)(.*?)(<idle2>)(.*?)(<value>)(.*?)(</value>)(.*?)(</idle2>)(.*?)(</fieldset>)
用于替换:
self.replacePattern=`\g<1>\g<2>\g<3>\g<4><value>denemeasdasd\\\\n</value>\g<8>\g<9>\g<10>\g<11>`
这是用于搜索的python代码:
self.pattern = re.compile(r''''''+self.searchPattern+'''''', flags = re.S | re.U)
这是用于替换
outtext = self.pattern.sub(r''''''+self.replacePattern+'''''',r''''''+self.match.group(0)+'''''')
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.
Think I have a xml file like below:
<fieldset>
<idle1>
<value>something\\n</value>
</idle1>
<idle2>
<value>blabla</value>
</idle2>
</fieldset>
If I try to replace value in "<idle2><value>
" node, value of "<idle1><value>
" node becomes "something\n". And when it comes to writing to file, xml becomes:
<fieldset>
<idle1>
<value>something
</value>
</idle1>
<idle2>
<value>blabla</value>
</idle2>
</fieldset>
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 "\\n
" and then I write result to the file. But it is not an efficient way to use.
Is there something I could not see? I just want to write "\\n
" to the files. Is this so much for me to want it?
Edit: here is my regexs':
for search :
self.searchPattern=(<fieldset>)(.*?)(<idle2>)(.*?)(<value>)(.*?)(</value>)(.*?)(</idle2>)(.*?)(</fieldset>)
for replace :
self.replacePattern=`\g<1>\g<2>\g<3>\g<4><value>denemeasdasd\\\\n</value>\g<8>\g<9>\g<10>\g<11>`
this is the python code for search:
self.pattern = re.compile(r''''''+self.searchPattern+'''''', flags = re.S | re.U)
and this is for replacing
outtext = self.pattern.sub(r''''''+self.replacePattern+'''''',r''''''+self.match.group(0)+'''''')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白你的解释。
就我个人而言,我写了这个:
结果
是你想要的吗?
I don't understand your explanations.
Personnaly, I wrote this:
result
Is it what you want ?
我发现在处理不可预测的数据源时最好将有效字符列入白名单。
因此,与您正在进行的任何其他正则表达式替换一起,删除任何未列入白名单的内容,即 az 0-9 : , 。 -
查看您的数据并确定适合您的任务的白名单。
I find it best when dealing with unpredictable data sources to whitelist valid characters.
So along with whatever other regular expression replace you have going on, remove anything that's not whitelisted i.e. a-z 0-9 : , . -
Look at your data and determine the appropriate whitelist for your task.