正则表达式瓦特/均衡组相匹配,不仅最外层的比赛 [英] Regex w/ balancing group that matches not only the outmost matches

查看:164
本文介绍了正则表达式瓦特/均衡组相匹配,不仅最外层的比赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过关于如何使用均衡组中多个数据源,但所有的例子都只是匹配最外层的比赛。是否有可能匹配匹配一个正则表达式的模式,所有的子? (Platform是.NET 4.0)

下面是什么,我希望有一个例子: 输入:

  

A +((B +(C + D))+(E + F))

期望匹配(在教科书正则表达式只生成了第一场比赛):

  
      
  • ((B +(C + D))+(E + F))
  •   
  • (B +(C + D))
  •   
  • (C + D)
  •   
  • (E + F)
  •   
解决方案

这是可以做到与正则表达式用超前的帮助。但是,这是不是最佳的,因为这将重新分析一些团体括号的每一场比赛。使用实解析器将读/分析字符串只有一次,将更有效率。

例( ideone ):

 使用系统;
使用System.Text.RegularEx pressions;

公共类实例
{
   公共静态无效的主要()
   {
      VAR重= @(?X)#忽略空格和注释
(?=#前瞻(零宽)
  (
    \(                 # 第一 (
    (?:
      (LT;开放> \()*#开++
      [^()] +
      (?<  - 开放> \))*#open--
    )+
    \)                 # 持续 )
    (?(开)(?!))#失败,如果unblanaced:开> 0
  )
)
\(#吃了(,推进匹配一个字符;

      变种海峡=A +((B +(C + D))+(E + F))+(X +((y)的+(z)的)+ X);

      VAR M = Regex.Matches(STR,重);

      Console.WriteLine(匹配);
      的foreach(比赛我米)
        Console.WriteLine(i.Groups [1]);
   }
}
 

输出:

 匹配:
((B +(C + D))+(E + F))
(B +(C + D))
(C + D)
(E + F)
(X +((y)的+(Z))+ x)的
((Y)+(Z))
(Y)
(z)的
 

I've read multiple sources on how to use balancing group, but all the examples are only matching the outmost matches. Is it possible to match all substrings that match the pattern with one RegEx? (Platform is .NET 4.0)

Here is an example of what I want: Input:

a + ((b + (c + d)) + (e + f))

Desired matches (the 'textbook' RegEx only generates the first match):

  • ((b + (c + d)) + (e + f))
  • (b + (c + d))
  • (c + d)
  • (e + f)

解决方案

This can be done with regex with help of a lookahead. But that isn't optimal as it would "reparse" some groups of parenthesis for every match. Using a real parser would read/parse the string only once, and would be more efficient.

Example (ideone):

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      var re = @"(?x)  # ignore spaces and comments
(?=                    # lookahead (zero width)
  (
    \(                 # first (
    (?:
      (?<open> \( )*   # open++
      [^()]+
      (?<-open> \) )*  # open--
    )+
    \)                 # last )
    (?(open)(?!))      # fail if unblanaced: open > 0
  )
)
\(                     # eat a (, to advance the match a char";

      var str = "a + ((b + (c + d)) + (e + f)) + (x + ((y) + (z)) + x)";

      var m = Regex.Matches(str, re);

      Console.WriteLine("Matched: ");
      foreach (Match i in m)
        Console.WriteLine(i.Groups[1]);
   }
}

Output:

Matched: 
((b + (c + d)) + (e + f))
(b + (c + d))
(c + d)
(e + f)
(x + ((y) + (z)) + x)
((y) + (z))
(y)
(z)

这篇关于正则表达式瓦特/均衡组相匹配,不仅最外层的比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆