import re
s = "In $ \mathcal{N}=4$ we fix them completely through the duality to null"
regex = re.compile(r'(?<=\$).+(?=\$)', re.M)
# returns you the string containing
# only the contents inside $ ... $
s = ' '.join(map(str.strip, regex.findall(s)))
print(s)
将
re.sub
直接与方法一起使用作为替换:见proof online
它有什么作用
(\$[^$]*\$)|[^\x00-\x7F]+|\(
使用(\$[^$]*\$)
将两个美元符号之间的所有字符捕获到ID为1的捕获组中,并在不捕获的情况下将所有非ASCII或(
字符与[^\x00-\x7F]+|\(
匹配李>m.group(1)
会将匹配的内容放回原处),否则,替换项是一个空格李>通过使用lookaround regex,我们可以匹配
$ ... $
中的所有内容了解有关lookaround运算符here的详细信息
相关问题 更多 >
编程相关推荐