找到RegEx匹配然后提取

时间:2016-05-15 16:46:35

标签: c# regex return-value

我正在尝试从RichTextBox中读取文本,以便找到匹配表达式的第一个匹配项。然后我想提取满足它们查询的字符串,这样我就可以将它用作变量。下面是我必须开始使用和构建的基本代码。

private string returnPostcode()
{
        string[] allLines = rtxtDocViewer.Text.Split('\n');
        string expression =  string expression = "^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$"            

        foreach (string line in allLines)
        {
            if (Regex.Matches(line, expression, RegexOptions.Count > 0)
            {                    
                //extract and return the string that is found
            }
        }
  }     

RichTextBox中包含的内容示例如下所示。我想提取上面的正则表达式应该能找到的" E12 8SD" 。感谢

Damon Brown
Flat B University Place
26 Park Square 
London
E12 8SD
Mobile: 1111 22222
Email: dabrown192882@gmail.com  Date of birth: 21/03/1986
Gender: Male
Marital Status: Single
Nationality: English
Summary
I have acquired a multifaceted skill set with experience using several computing platforms.

1 个答案:

答案 0 :(得分:1)

您需要使用Regex.IsMatch并删除RegexOptions.Count > 0

string[] allLines = s.Split('\n');
string expression = "^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$";

foreach (string line in allLines)
{
    if (Regex.IsMatch(line, expression)) // Regex.IsMatch will check if a string matches the regex
    {                     
        Console.WriteLine(line);         // Print the matched line
    }
}

请参阅IDEONE Demo

您的文字很可能包含CR + LF换行符。然后,按如下方式调整代码:

string[] allLines = s.Split(new[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);

请参阅this demo

<强>更新

要使用正则表达式提取代码,您无需将内容拆分为行,只需在整个文本中使用Regex.Match

string s = "Damon Brown\nFlat B University Place\n26 Park Square \nLondon\nTW1 1AJ Twickenham    Mobile: +44 (0) 7711223344\nMobile: 1111 22222\nEmail: dabrown192882@gmail.com    Date of birth: 21/03/1986\nGender: Male\nMarital Status: Single\nNationality: English\nSummary\nI have acquired a multifaceted skill set with experience using several computing platforms."; 
string expression = @"(?i)\b(gir 0a{2})|((([a-z][0-9]{1,2})|(([a-z][a-hj-y][0-9]{1,2})|(([a-z][0-9][a-z])|([a-z][a-hj-y][0-9]?[a-z])))) [0-9][a-z]{2})\b";
Match res = Regex.Match(s, expression);
if (res.Success)
    Console.WriteLine(res.Value); // = > TW1 1AJ

我还删除了大写范围,用不区分大小写的修饰符(?i)替换它们。

请参阅this IDEONE demo