I want to retrieve substrings from a string using regex.
Just for info : These string values are subjects in mails
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"
Substrings:
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>
How can i write regex(either seperately or one regex for both) for the above two strings to get the same output.
I think match groups can be used to retrieve the data. But i am quite new to regex.
Try this:
// 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,
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With