【问题标题】:java, unable to understand the concept of back-referencing within the context of nested capturing groupjava,无法理解嵌套捕获组上下文中的反向引用的概念
【发布时间】:2016-06-17 13:44:55
【问题描述】:

我是 Java 新手,正在尝试学习捕获组的概念。我在 Java 教程 oracle 上阅读:

对于嵌套的捕获组,反向引用的工作原理完全相同 方式:指定一个反斜杠,后跟要成为的组的编号 召回。

由于我的经验不足,我无法从概念上理解它,并且教程没有提供任何代码示例。有人可以向我解释这个概念吗,最好用一些代码?在此先感谢您的帮助

【问题讨论】:

    标签: java regex capturing-group


    【解决方案1】:

    我认为他们在谈论这个

     (                             # (1 start)
          nested
          (                             # (2 start)
               groups  
          )                             # (2 end)
     )                             # (1 end)
     \s     
     (                             # (3 start)
          \1 
          \s 
          \2+ 
     )                             # (3 end)
    

    您可以看到第 1 组包含第 2 组。

    然后您可以使用反向引用引用第 1 组中匹配的内容
    这也将包含第 2 组中匹配的内容。

     **  Grp 0 -  ( pos 0 , len 38 ) 
    nestedgroups nestedgroups groupsgroups  
     **  Grp 1 -  ( pos 0 , len 12 ) 
    nestedgroups  
     **  Grp 2 -  ( pos 6 , len 6 ) 
    groups  
     **  Grp 3 -  ( pos 13 , len 25 ) 
    nestedgroups groupsgroups  
    

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多