普通EX pression选择重复组 [英] Regular expression to select repeating groups

查看:118
本文介绍了普通EX pression选择重复组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列遵循特定的格式,并想用一个单一的EX pression捕捉到组分组值。 例如,我有 -group1 -group2 -group3 和想使用类似的东西来( - [\ S \ S] {1,} ?)这基本上让我捕捉到整个字符串成一个组,但我希望能够分别反向引用的每个值。我想通了将迫使它是不贪婪,因此,拆分模式匹配为三个独立的小组(例如)。 现在我只是重复引用( - ?[\ S \ S] *)。,但它似乎应该有一个更优雅的前pression
谢谢!

I have a series of grouped values that follow a specific format and would like to use a single expression to capture them into groups. For example, I have -group1 -group2 -group3 and am trying to use something similar to (-[\s\S]{1,}?) This is basically allowing me to capture the entire string into a single group but I'd like to be able to backreference each of the values separately. I figured the ? would force it to be non-greedy and, therefore, split the pattern match into three separate groups (for example). For now I am simply repeating the reference (-[\s\S]*?) but it seems there should be a more elegant expression.
Thanks!

推荐答案

您是幸运的,因为C#是为数不多的语言之一(如果不是唯一的),支持SUBEX pression捕获

You are in luck because C# is one of the few languages (if not the only one) that supports subexpression captures

<一个href="http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.capture(v=vs.110">http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.capture(v=vs.110)

在.NET API可以看如下:

The .NET API can be looked at as follows

 Matches
     Groups (most regex engines stop here)
         Captures (unique for .NET)

这不是你想匹配正是从你的问题清楚,但这应该让你开始。再问,如果你被卡住。

It's not clear from your question what you want to match exactly but this should get you started. Ask again if you are stuck.

  string input = "-group1 -group2 ";
  string pattern = @"(-\S*\W){2}";
  foreach (Match match in Regex.Matches(input, pattern))
  {
     Console.WriteLine("Match: {0}", match.Value);
     for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
     {
        Group group = match.Groups[groupCtr];
        Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
        for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
           Console.WriteLine("      Capture {0}: {1}", captureCtr, 
                             group.Captures[captureCtr].Value);
     }                      
  } 

本。OUPUTS

Match: -group1 -group2 
   Group 0: -group1 -group2 
      Capture 0: -group1 -group2 
   Group 1: -group2 
      Capture 0: -group1 
      Capture 1: -group2 

正如你所看到的(第1组,捕捉0)和(第1组,捕获1)提供一组独立捕获(而不是最后的大多数语言)

As you can see (Group 1, Capture 0) and (Group 1, Capture 1) offer the individual captures of a group (and not the last as in most languages)

这个地址我觉得你的描述为以能够分别反向引用的每个值

This address I think of what you describe as "to be able to backreference each of the values separately"

(你用这个词反向引用,但我不认为你是对的目标是替换模式?)

(You use the term backreference but I don't think you are aiming for a replacement pattern right?)

这篇关于普通EX pression选择重复组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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