簡體   English   中英

正確的Regex-Syntax for Python中的re.sub替換

[英]Correct Regex-Syntax for re.sub replacement in Python

我正在尋找正確的正則表達式來替換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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM