简体   繁体   English

如何在python中一起替换字符串,换行和空格

[英]How to replace string, new line and spaces together in python

I have a long string called webpage contains something like this: 我有一个很长的字符串,称为网页,其中包含如下内容:

"<!--  \n    <div class=\"section_content\"> \n    </div>\n\n-->  "

I want to replace the comments symbol "<!--" and "-->" with spaces. 我想用空格替换注释符号"<!--""-->" However I cannot directly replace them, since there are other real comments, like "<!-- comments -->" , in the long string. 但是我不能直接替换它们,因为长字符串中还有其他真实的注释,例如"<!-- comments -->"

I was trying to use 我正在尝试使用

re.sub(r"<!--\s+\n\s+<div",r"\n<div",webpage,flags=re.MULTILINE)

But it does not work at all. 但这根本不起作用。 Can someone help? 有人可以帮忙吗? The result should be "\\n <div class=\\"section_content\\"> \\n </div>\\n\\n" . 结果应为"\\n <div class=\\"section_content\\"> \\n </div>\\n\\n"

This should do: 应该这样做:

import re

regex = r"<!--(\s*\n\s*<div[^>]*>\s*\n\s*</div>\n\n)-->"
string = "<!--  \n    <div class=\"section_content\"> \n    </div>\n\n-->  "
res = re.sub(regex, r"\1", string)
print res

Result: 结果:

"  \n    <div class=\"section_content\"> \n    </div>\n\n"

Then, if you don't want newlines and spaces at the ends of the string you can use the .strip() method of the string object. 然后,如果您不想在字符串的.strip()使用换行符和空格,则可以使用字符串对象的.strip()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM