如何阅读在C#中的正则表达式捕获 [英] How to read RegEx Captures in C#

查看:112
本文介绍了如何阅读在C#中的正则表达式捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一个C#的书,我决定正则表达式的扔到组合,使枯燥的练习控制台稍微更有趣。我想要做的就是索要他们的电话号码,用户在控制台中,检查对正则表达式,然后捕获的数字,所以我可以格式化他们,我想要的方式。我已经得到了所有的工作,除了正则表达式捕获的一部分。如何获取捕捉值到C#的变量?

也随时纠正任何code格式或变量命名的问题。

 静态无效askPhoneNumber()
{
    字符串模式= @[(](\\ d {3})?[)] [ - ](\\ D {3}) - (\\ d {4})?[]?;    的System.Console.WriteLine(什么是你的电话号码吗?);
    字符串phoneNumber的=到Console.ReadLine();    而(!Regex.IsMatch(phoneNumber的,图案))
    {
        Console.WriteLine(坏输入);
        phoneNumber的=到Console.ReadLine();
    }    匹配匹配= Regex.Match(phoneNumber的,图案);
    捕捉捕捉= match.Groups.Captures;    的System.Console.WriteLine(捕捉[1] .value的+ - +捕捉[2] .value的+ - +捕捉[3]。价值);
}


解决方案

C#的正则表达式API可能会相当混乱。还有的集团的和的捕获的:


  • A 的再presents捕获组,它被用来从文本
  • 抽取子
  • 可以有多个的捕获的每个组,如果组一个量词内出现。

的层次是:


  • 匹配

    • 集团

      • 捕获



(匹配可以有多个组,每个组可以有几个捕获)

例如:

 主题:aabcabbc
图案:^ C)+ $(:( A + B +?)

在这个例子中,只有一个组:(A + B +)。这组是一个量词内,并匹配两次。它生成两个的捕获 AAB 羊毛

  aabcabbc
^^^ ^^^
CAP1 CAP2

在一组不是一个数量里面,它会产生只有一个捕获。你的情况,你有3组,每组拍摄一次。您可以使用 match.Groups [1] .value的 match.Groups [2] .value的 match.Groups [3] .value的来提取3子你有兴趣,而不诉诸的捕获的概念都没有。

I started a C# book and I decided to throw RegEx's into the mix to make the boring console exercises a little more interesting. What I want to do is ask a user for their phone number in the console, check it against a RegEx, then capture the digits so I can format them the way I want. I've got all that working except the RegEx capture part. How do I get the capture values into C# variables?

Also feel free to correct any code formatting or variable naming issues.

static void askPhoneNumber()
{
    String pattern = @"[(]?(\d{3})[)]?[ -.]?(\d{3})[ -.]?(\d{4})";

    System.Console.WriteLine("What is your phone number?");
    String phoneNumber = Console.ReadLine();

    while (!Regex.IsMatch(phoneNumber, pattern))
    {
        Console.WriteLine("Bad Input");
        phoneNumber = Console.ReadLine();
    }

    Match match = Regex.Match(phoneNumber, pattern);
    Capture capture = match.Groups.Captures;

    System.Console.WriteLine(capture[1].Value + "-" + capture[2].Value + "-" + capture[3].Value);
}

解决方案

The C# regex API can be quite confusing. There are groups and captures:

  • A group represents a capturing group, it's used to extract a substring from the text
  • There can be several captures per group, if the group appears inside a quantifier.

The hierarchy is:

  • Match
    • Group
      • Capture

(a match can have several groups, and each group can have several captures)

For example:

Subject: aabcabbc
Pattern: ^(?:(a+b+)c)+$

In this example, there is only one group: (a+b+). This group is inside a quantifier, and is matched twice. It generates two captures: aab and abb:

aabcabbc
^^^ ^^^
Cap1  Cap2

When a group is not inside of a quantifier, it generates only one capture. In your case, you have 3 groups, and each group captures once. You can use match.Groups[1].Value, match.Groups[2].Value and match.Groups[3].Value to extract the 3 substrings you're interested in, without resorting to the capture notion at all.

这篇关于如何阅读在C#中的正则表达式捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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