我正在尝试创建一个简单的降价乳胶转换器,只是为了学习
python和基本的正则表达式,但我不知道试图弄清楚为什么下面的代码不起作用:
re.sub (r'\[\*\](.*?)\[\*\]: ?(.*?)$', r'\\footnote{\2}\1', s, flags=re.MULTILINE|re.DOTALL)
我想转换像:
s = """This is a note[*] and this is another[*]
[*]: some text
[*]: other text"""
至:
This is a note\footnote{some text} and this is another\footnote{other text}
这就是我得到的(使用上面的正则表达式):
This is a note\footnote{some text} and this is another[*]
[*]: note 2
为什么模式只匹配一次?
编辑:
我尝试了以下先行断言:
re.sub(r'\[\*\](?!:)(?=.+?\[\*\]: ?(.+?)$',r'\\footnote{\1}',flags=re.DOTALL|re.MULTILINE)
#(?!:) is to prevent [*]: to be matched
现在它匹配所有脚注,但它们没有正确匹配.
s = """This is a note[*] and this is another[*]
[*]: some text
[*]: other text"""
给了我
This is a note\footnote{some text} and this is another\footnote{some text}
[*]: note 1
[*]: note 2
有什么想法吗?