我目前正在寻找正确的正则表达式来替换 Python 中的类似内容:
old_string 包含:
some text
some text
<!-V
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat
V->
some text
我尝试用什么来替换 <!-V *** V->
部分:
import re
new_string = re.sub(r"<!-V(.*?)V->", '', old_string)
但它似乎不正确,也许任何人都可以帮助我使用正确的正则表达式。
最佳答案
默认情况下,.
不匹配换行符。如果您希望 .
也匹配这些,请使用 DOTALL
标志:
new_string = re.sub(r"<!-V(.*?)V->", '', old_string, flags=re.DOTALL)
还有一个选项可以将 .
替换为匹配所有字符的字符类,方法是组合两个互补的字符类:
[\s\S]
关于python - 在 Python 中更正用于 re.sub 替换的正则表达式语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25058613/