2

我在许多正则表达式引擎中发现了一些有趣的可能性:

可以在捕获组内放置反向引用并引用该组。

例如:(\1)

我的问题:它可以用于哪些正则表达式模式?我无法想象...

4

3 回答 3

3

有一个关于使用嵌套引用的解释,我认为这清楚地表明了这个特性的边际有用性。

于 2010-02-04T12:54:03.857 回答
1

我有一个很长的 yaml 模式,我需要/想要改变它:

columns:
    id:
      type: string(36)
      fixed: true
      unsigned: false
      primary: true
      autoincrement: false
    descripcion:
      type: string(45)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false

对此:

columns:
    id: {type: string(36), fixed: true, unsigned: false, primary: true, autoincrement: false}
    descripcion: {...}...

在这种情况下,我需要在反向引用中进行一定程度的递归,因为列名位于列描述之间。这适用于每一行,但我不能把“{}”放在 $1 周围,我会在每个描述字段之间加上括号:

"\n^      ([^\n]+)"
于 2011-02-27T14:09:31.903 回答
0

这个怎么样(忽略空格):

{ .* ( { .* \1 .* } ) .* }

匹配 C#/Java/... 中方法的主体

于 2010-02-24T19:25:47.943 回答