我使用python和正则表达式来尝试提取可选的字符串中间。找到可选中间的字符串包围懒惰,正则表达式
>>> re.search(r'(.*?)(HELLO|BYE)?(.*?END)', r'qweHELLOsdfsEND').groups()
('', None, 'qweHELLOsdfsEND') #what I want is ('qwe', 'HELLO', 'sdfsEND')
>>> re.search(r'(.*?)(HELLO|BYE)?(.*?END)', r'qweBLAHsdfsEND').groups()
('', None, 'qweBLAHsdfsEND') #when the middle doesn't match. this is OK
我怎样才能提取可选的中间?
注意:这是我的第一篇文章。
是的,可以解决我的问题!我以前没有遇到过(?!)。 – chobok
看看这个[教程](http://www.regular-expressions.info/lookaround.html)。 Lookaround是一个很棒的功能。 –
这是和优秀的教程。真的很好写和全面。谢谢 – chobok