3

我想使用正则表达式从字符串中检索子字符串。

仅供参考:这些字符串值是邮件中的主题

String1 = "Acceptance :DT_Ext_0062-12_012ed2 [Describe]"

string2 = "Acceptance : DT_Ext_0062-12_012 (ed.2) , Describe"

string3 = "Acceptance of : DT_Ext_0062-12_012 (ed.2) , Describe to me"

子串:

sub1 = Acceptance            <Mail Type : like Reject or Accept>
sub2 = DT_Ext_0062-12_012    <ID : unique identifier>
sub3 = ed2                   <Edition of mail, like : ed1, ed2, ed3 ...so on>
sub4 = Describe              <Description of the mail>

我如何为上述两个字符串编写正则表达式(单独或一个正则表达式)以获得相同的输出。

我认为匹配组可用于检索数据。但我对正则表达式很陌生。

4

1 回答 1

1

尝试这个:

// string strTargetString = @"Acceptance :DT_Ext_0062-12_012ed2 [Describe]";
// string strTargetString = @"Acceptance : DT_Ext_0062-12_012 (ed.2) , Describe";
string strTargetString = @"Acceptance of : DT_Ext_0062-12_012 (ed.2) , Describe to me";

 const string strRegex = @"\.*:\s*(DT_Ext_\d{4}-\d{2}_\d{3})\s*\W*(ed)\.?(\d+)(\W*[,])?(.*)";


RegexOptions myRegexOptions = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.CultureInvariant;
Regex myRegex = new Regex(strRegex, myRegexOptions);


foreach(Match myMatch in myRegex.Matches(strTargetString))
{
    if(myMatch.Success)
    {
        // Add your code here
        var value = new {
            Value1 = myMatch.Groups[1].Value,
            Value2 = myMatch.Groups[2].Value,
            Value3 = myMatch.Groups[3].Value,
            Value4 = myMatch.Groups[5].Value,
        };
    }
}
于 2013-08-16T10:10:42.037 回答