【发布时间】:2018-08-15 15:19:39
【问题描述】:
我有以下“字符串”:
{ see 'identifier' }
Some Text
{ see 'otherid' }
Another Piece of Text
{ /see }
{ /see }
我想“提取”列表中的开始/结束,当然是考虑正则表达式。 现在我做:
(\{ see([\s\S]+?)\}([\s\S]*?)\{ \/see \})
结果:
Match 1
1. { see 'identifier' } Some Text { see 'otherid' } Another Piece of Text { /see }
2. 'identifier'
3. Some Text { see 'otherid' } Another Piece of Text
但是,我希望能抓到两场比赛……
Match 1:
1. { see 'identifier' } Some Text { see 'otherid' } Another Piece of Text { /see } { /see }
2. 'identifier'
3. Some Text { see 'otherid' } Another Piece of Text { /see }
And Match 2:
1. { see 'otherid' } Another Piece of Text { /see }
2. 'otherid'
3. Another Piece of Text
这可能在单个正则表达式中,还是我应该对此有不同的想法?
如果需要,这在 Py3.4+ 中,没有框架或任何可用的原生模块。可以安装 Pip,但不是首选。谢谢!
【问题讨论】:
-
使用正则表达式,您只能使用 PyPi
regex模块来实现。使用re,您不能使用递归模式。我宁愿在这里使用解析器。 -
好建议,非常感谢。
标签: python regex python-3.x pattern-matching