【问题标题】:Regex for Python with nested results具有嵌套结果的 Python 正则表达式
【发布时间】: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


【解决方案1】:

好的,所以当我们匹配字符串的结尾时,像这样:

(\{ see([\s\S]+?)\}([\s\S]*?)\{ \/see \}$)

按预期返回第一个匹配项,但不返回第二个匹配项。

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 }

这仍然不是我想要的,但如果需要的话会做的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    相关资源
    最近更新 更多