Python 如何将子字符串括在方括号中
我有一个很长的文本,其中的部分用+++括起来,我想用方括号括起来Python 如何将子字符串括在方括号中,python,regex,re,Python,Regex,Re,我有一个很长的文本,其中的部分用+++括起来,我想用方括号括起来 se1 = "+++TEXT:+++ Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi - community. +++ : Bnei Brak, Tel Aviv + Jerusalem ))+++" 我想
se1 = "+++TEXT:+++ Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi - community. +++ : Bnei Brak, Tel Aviv + Jerusalem ))+++"
我想将+++中包含的文本转换为[[]],因此
+++TEXT+++ should become [[TEXT]]
我的代码:
import re
se1 = "+++TEXT:+++ Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi - community. +++ Karte Israel mit: Bnei Brak, Tel Aviv + Jerusalem ))+++"
comments = re.sub(r"\+\+\+.*?\+\+\+", r"[[.*?]]", se1)
print(comments)
但它给出了错误的输出
[[.*?]] Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi - community. [[.*?]]
您需要使用
()
捕获组,然后使用\1
引用匹配的组
这应该很好:
>>> comments = re.sub(r"\+\+\+(.*?)\+\+\+", r"[[\1]]", se1)
>>> comments
'[[TEXT:]] Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi - community. [[ Karte Israel mit: Bnei Brak, Tel Aviv + Jerusalem ))]]'
考虑到
\+\++
也可以简化为\+{3}
。您需要用()
捕获组,然后用\1
引用匹配组
这应该很好:
>>> comments = re.sub(r"\+\+\+(.*?)\+\+\+", r"[[\1]]", se1)
>>> comments
'[[TEXT:]] Moshe Morgenstern is on his way to the main synagogue in the center of Bnei Brak, home to a largely ultra-orthodox - or haredi - community. [[ Karte Israel mit: Bnei Brak, Tel Aviv + Jerusalem ))]]'
考虑到\+\++
也可以简化为\+{3}
。您可以使用以下方法:
re.sub(r'\+\+\+(.*?)\+\+\+',r'[[\1]]',se1)
由于第二个字符串中的*?
被视为纯字符串,而不是匹配字符串中*?
的替换,因此(.*)
意味着保存此部分以用于替换字符串,而\1
是保存的数据。您可以使用以下方法:
re.sub(r'\+\+\+(.*?)\+\+\+',r'[[\1]]',se1)
由于第二个字符串中的*?
被视为纯字符串,而不是匹配字符串中*?
的替换,因此(.*)
意味着保存此部分以用于替换字符串,而\1
是保存的数据