Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中读取正则表达式捕获#_C#_Regex_Console - Fatal编程技术网

C# 如何在C中读取正则表达式捕获#

C# 如何在C中读取正则表达式捕获#,c#,regex,console,C#,Regex,Console,我开始写一本C#book,我决定将正则表达式加入其中,让枯燥的控制台练习更有趣一些。我想做的是让用户在控制台中输入他们的电话号码,对照正则表达式进行检查,然后捕获数字,这样我就可以按照我想要的方式对其进行格式化。除正则表达式捕获部分外,所有这些都在工作。如何将捕获值转换为C#变量 也可以随意更正任何代码格式或变量命名问题 static void askPhoneNumber() { String pattern = @"[(]?(\d{3})[)]?[ -.]?(\d{3})[ -.]?

我开始写一本C#book,我决定将正则表达式加入其中,让枯燥的控制台练习更有趣一些。我想做的是让用户在控制台中输入他们的电话号码,对照正则表达式进行检查,然后捕获数字,这样我就可以按照我想要的方式对其进行格式化。除正则表达式捕获部分外,所有这些都在工作。如何将捕获值转换为C#变量

也可以随意更正任何代码格式或变量命名问题

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);
}

比赛结果可能很难理解。我写这段代码是为了帮助我理解在哪里发现了什么。其目的是可以将输出的片段(从标有
/**
的行中)复制到程序中,以利用匹配中找到的值

public static void DisplayMatchResults(Match match)
{
    Console.WriteLine("Match has {0} captures", match.Captures.Count);

    int groupNo = 0;
    foreach (Group mm in match.Groups)
    {
        Console.WriteLine("  Group {0,2} has {1,2} captures '{2}'", groupNo, mm.Captures.Count, mm.Value);

        int captureNo = 0;
        foreach (Capture cc in mm.Captures)
        {
            Console.WriteLine("       Capture {0,2} '{1}'", captureNo, cc);
            captureNo++;
        }
        groupNo++;
    }

    groupNo = 0;
    foreach (Group mm in match.Groups)
    {
        Console.WriteLine("    match.Groups[{0}].Value == \"{1}\"", groupNo, match.Groups[groupNo].Value); //**
        groupNo++;
    }

    groupNo = 0;
    foreach (Group mm in match.Groups)
    {
        int captureNo = 0;
        foreach (Capture cc in mm.Captures)
        {
            Console.WriteLine("    match.Groups[{0}].Captures[{1}].Value == \"{2}\"", groupNo, captureNo, match.Groups[groupNo].Captures[captureNo].Value); //**
            captureNo++;
        }
        groupNo++;
    }
}
使用此方法的一个简单示例,给出以下输入:

Regex regex = new Regex("/([A-Za-z]+)/(\\d+)$");
String text = "some/directory/Pictures/Houses/12/apple/banana/"
            + "cherry/345/damson/elderberry/fig/678/gooseberry");
Match match = regex.Match(text);
DisplayMatchResults(match);
输出为:

Match has 1 captures
  Group  0 has  1 captures '/Houses/12'
       Capture  0 '/Houses/12'
  Group  1 has  1 captures 'Houses'
       Capture  0 'Houses'
  Group  2 has  1 captures '12'
       Capture  0 '12'
    match.Groups[0].Value == "/Houses/12"
    match.Groups[1].Value == "Houses"
    match.Groups[2].Value == "12"
    match.Groups[0].Captures[0].Value == "/Houses/12"
    match.Groups[1].Captures[0].Value == "Houses"
    match.Groups[2].Captures[0].Value == "12"
假设我们希望在上面的文本中找到上述正则表达式的所有匹配项。然后我们可以在代码中使用
MatchCollection
,例如:

MatchCollection matches = regex.Matches(text);
for (int ii = 0; ii < matches.Count; ii++)
{
    Console.WriteLine("Match[{0}]  // of 0..{1}:", ii, matches.Count-1);
    RegexMatchDisplay.DisplayMatchResults(matches[ii]);
}
因此:

    matches[1].Groups[0].Value == "/cherry/345/"
    matches[1].Groups[1].Value == "cherry"
    matches[1].Groups[2].Value == "345"
    matches[1].Groups[0].Captures[0].Value == "/cherry/345/"
    matches[1].Groups[1].Captures[0].Value == "cherry"
    matches[1].Groups[2].Captures[0].Value == "345"
类似地,对于
匹配项[0]
匹配项[2]

来说,C#regex API可能会非常混乱。有组和捕获:

  • 组表示捕获组,用于从文本中提取子字符串
  • 如果组出现在量词中,则每个组可以有多个捕获
层次结构是:

  • 匹配
    • 团体
      • 俘获
(一个匹配可以有多个组,每个组可以有多个捕获)

例如:

Subject: aabcabbc
Pattern: ^(?:(a+b+)c)+$
在本例中,只有一个组:
(a+b+)
。该组位于量词内,匹配两次。它生成两个捕获:
aab
abb

aabcabbc
^^^ ^^^
Cap1  Cap2
当一个组不在量词内时,它只生成一个捕获。在本例中,您有3个组,每个组捕获一次。您可以使用
match.Groups[1].Value
match.Groups[2].Value
match.Groups[3].Value
提取您感兴趣的3个子字符串,而无需使用捕获概念

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();
}

var match = Regex.Match(phoneNumber, pattern);
if (match.Groups.Count == 4)
{
    System.Console.WriteLine("Number matched : "+match.Groups[0].Value);
    System.Console.WriteLine(match.Groups[1].Value + "-" + match.Groups[2].Value + "-" + match.Groups[3].Value);
}
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();
}

var match = Regex.Match(phoneNumber, pattern);
if (match.Groups.Count == 4)
{
    System.Console.WriteLine("Number matched : "+match.Groups[0].Value);
    System.Console.WriteLine(match.Groups[1].Value + "-" + match.Groups[2].Value + "-" + match.Groups[3].Value);
}