RegEx not finding matches ... but it should
-
Hey All, I am experiencing a strange issue, i have built and tested my RegEx in Expresso but when I call it from my code (C#) I am not getting any matches. If i step through and grab the values from the variables and put those in Expresso it works .... RegEx:
Lead\sID\sNumber:\s*(?<LeadNumber>[^\r\n]*)(?:\r\n)+ Prospect\sName:.*(?:\r\n)+ Prospect\sContact:.*(?:\r\n)+ Prospect\sPhone:.*(?:\r\n)+ Marketing\sCampaign:.*(?:\r\n)+ Prospect\sInformation:\s*(?:\r\n)+ ===============\s(?:\r\n)+ (?<Company>[^\r\n]*)(?:\r\n)+ (?<Address1>[^,]*),\s(?<Address2>[^\r\n]*)(?:\r\n)+ (?<City>[^,]*),\s(?<State>[^\r\n]*)(?:\r\n)+ (?<Country>[^,]*),\s(?<Zip>[^\r\n]*)(?:\r\n)+ Contact\sName:\s*(?<FirstName>[^\s]*)\s(?<LastName>[^\r\n]*)(?:\r\n)+ Contact\sPhone:\s*(?<Phone>[^\r\n]*)(?:\r\n)+
C#:MatchCollection myMatches = Regex.Matches([Text], [RegEx], RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.CultureInvariant);
[Text] = the body of an email [RegEx] = the above pattern Any input or ideas would be greatly appreciated. -
Hey All, I am experiencing a strange issue, i have built and tested my RegEx in Expresso but when I call it from my code (C#) I am not getting any matches. If i step through and grab the values from the variables and put those in Expresso it works .... RegEx:
Lead\sID\sNumber:\s*(?<LeadNumber>[^\r\n]*)(?:\r\n)+ Prospect\sName:.*(?:\r\n)+ Prospect\sContact:.*(?:\r\n)+ Prospect\sPhone:.*(?:\r\n)+ Marketing\sCampaign:.*(?:\r\n)+ Prospect\sInformation:\s*(?:\r\n)+ ===============\s(?:\r\n)+ (?<Company>[^\r\n]*)(?:\r\n)+ (?<Address1>[^,]*),\s(?<Address2>[^\r\n]*)(?:\r\n)+ (?<City>[^,]*),\s(?<State>[^\r\n]*)(?:\r\n)+ (?<Country>[^,]*),\s(?<Zip>[^\r\n]*)(?:\r\n)+ Contact\sName:\s*(?<FirstName>[^\s]*)\s(?<LastName>[^\r\n]*)(?:\r\n)+ Contact\sPhone:\s*(?<Phone>[^\r\n]*)(?:\r\n)+
C#:MatchCollection myMatches = Regex.Matches([Text], [RegEx], RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.CultureInvariant);
[Text] = the body of an email [RegEx] = the above pattern Any input or ideas would be greatly appreciated. -
Thank you for you reply, Here is a sample of the text to match.
Lead Overview: =========== Lead ID Number: 1234567 Prospect Name: Some Prospect Prospect Contact: Some Contact Prospect Phone: 1234567890 Marketing Campaign: Marketing Campaign Example Prospect Information: =============== Company Name Address1, Address2 New York, NY United States, 12345 Contact Name: Some Contact Contact Phone: 1234567890 Company Email: contact@email.com
-
Hey All, I am experiencing a strange issue, i have built and tested my RegEx in Expresso but when I call it from my code (C#) I am not getting any matches. If i step through and grab the values from the variables and put those in Expresso it works .... RegEx:
Lead\sID\sNumber:\s*(?<LeadNumber>[^\r\n]*)(?:\r\n)+ Prospect\sName:.*(?:\r\n)+ Prospect\sContact:.*(?:\r\n)+ Prospect\sPhone:.*(?:\r\n)+ Marketing\sCampaign:.*(?:\r\n)+ Prospect\sInformation:\s*(?:\r\n)+ ===============\s(?:\r\n)+ (?<Company>[^\r\n]*)(?:\r\n)+ (?<Address1>[^,]*),\s(?<Address2>[^\r\n]*)(?:\r\n)+ (?<City>[^,]*),\s(?<State>[^\r\n]*)(?:\r\n)+ (?<Country>[^,]*),\s(?<Zip>[^\r\n]*)(?:\r\n)+ Contact\sName:\s*(?<FirstName>[^\s]*)\s(?<LastName>[^\r\n]*)(?:\r\n)+ Contact\sPhone:\s*(?<Phone>[^\r\n]*)(?:\r\n)+
C#:MatchCollection myMatches = Regex.Matches([Text], [RegEx], RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.CultureInvariant);
[Text] = the body of an email [RegEx] = the above pattern Any input or ideas would be greatly appreciated.I assume that you are setting some variable (e.g. myRE) to the regex string. Are you using a verbatim string i.e.
myRE = @"Lead....(\r\n)+"
or just doingmyRE = "Lead...(\r\n)+"
? I think it should be a verbatim string otherwise \ is interpreted before Regex.Matches sees it.Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
I assume that you are setting some variable (e.g. myRE) to the regex string. Are you using a verbatim string i.e.
myRE = @"Lead....(\r\n)+"
or just doingmyRE = "Lead...(\r\n)+"
? I think it should be a verbatim string otherwise \ is interpreted before Regex.Matches sees it.Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
Thank you for your reply. Yes it is "Verbatum" assinged using @
If at first you don't succeed ... post it on The Code Project and Pray.
-
Thank you for your reply. Yes it is "Verbatum" assinged using @
If at first you don't succeed ... post it on The Code Project and Pray.
-
Thank you for you reply, Here is a sample of the text to match.
Lead Overview: =========== Lead ID Number: 1234567 Prospect Name: Some Prospect Prospect Contact: Some Contact Prospect Phone: 1234567890 Marketing Campaign: Marketing Campaign Example Prospect Information: =============== Company Name Address1, Address2 New York, NY United States, 12345 Contact Name: Some Contact Contact Phone: 1234567890 Company Email: contact@email.com
Your regex works fine. It is finding the match.
string sMatch = @" Lead Overview:
Lead ID Number: 1234567
Prospect Name: Some Prospect
Prospect Contact: Some Contact
Prospect Phone: 1234567890Marketing Campaign: Marketing Campaign Example
Prospect Information:
Company Name
Address1, Address2
New York, NY
United States, 12345Contact Name: Some Contact
Contact Phone: 1234567890
Company Email: contact@email.com";bool IsMatchFound = Regex.IsMatch(sMatch, @"Lead\sID\sNumber:\s*(?[^\r\n]*)(?:\r\n)+Prospect\sName:.*(?:\r\n)+Prospect\sContact:.*(?:\r\n)+Prospect\sPhone:.*(?:\r\n)+Marketing\sCampaign:.*(?:\r\n)+Prospect\sInformation:\s*(?:\r\n)+===============\s(?:\r\n)+(?[^\r\n]*)(?:\r\n)+(?[^,]*),\s(?[^\r\n]*)(?:\r\n)+(?[^,]*),\s(?[^\r\n]*)(?:\r\n)+(?[^,]*),\s(?[^\r\n]*)(?:\r\n)+Contact\sName:\s*(?[^\s]*)\s(?[^\r\n]*)(?:\r\n)+Contact\sPhone:\s*(?[^\r\n]*)(?:\r\n)+");
-
Just noticed that there's no test for "===========" after the Lead line. Is that the problem?
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
Hi David, You are right, nice catch. However that shouldn't effect the matching. Now I did solve my problem, look above for the solution. Thanks for your help.
If at first you don't succeed ... post it on The Code Project and Pray.
-
Your regex works fine. It is finding the match.
string sMatch = @" Lead Overview:
Lead ID Number: 1234567
Prospect Name: Some Prospect
Prospect Contact: Some Contact
Prospect Phone: 1234567890Marketing Campaign: Marketing Campaign Example
Prospect Information:
Company Name
Address1, Address2
New York, NY
United States, 12345Contact Name: Some Contact
Contact Phone: 1234567890
Company Email: contact@email.com";bool IsMatchFound = Regex.IsMatch(sMatch, @"Lead\sID\sNumber:\s*(?[^\r\n]*)(?:\r\n)+Prospect\sName:.*(?:\r\n)+Prospect\sContact:.*(?:\r\n)+Prospect\sPhone:.*(?:\r\n)+Marketing\sCampaign:.*(?:\r\n)+Prospect\sInformation:\s*(?:\r\n)+===============\s(?:\r\n)+(?[^\r\n]*)(?:\r\n)+(?[^,]*),\s(?[^\r\n]*)(?:\r\n)+(?[^,]*),\s(?[^\r\n]*)(?:\r\n)+(?[^,]*),\s(?[^\r\n]*)(?:\r\n)+Contact\sName:\s*(?[^\s]*)\s(?[^\r\n]*)(?:\r\n)+Contact\sPhone:\s*(?[^\r\n]*)(?:\r\n)+");
Hello D@nish, First of all i would like to thank both you and Riced. I have found the solution to my problem and its still kinda strange. There was actually two problems;
- for some strange reason I cant declare my RegEx string using a literal (using @ when assiging the string) if the literal spans multiple lines, for some reason this is messing with my RegEx. Which is why i guess in your example its working.
- the emails are being retrieved from Exchange via WebDAV and for some strange reason line breaks are only coming back as '\n' not '\r\n' like they should. So if I do a .Replace("\n","\r\n") on the body everything works fine. I guess this wasnt a problem for your in your example because the email body was assigned as a literal and therefore had the "proper" line breaks (\r\n)
Thanks again for your help guys, i never would have found this without your help.
If at first you don't succeed ... post it on The Code Project and Pray.
-
Hello D@nish, First of all i would like to thank both you and Riced. I have found the solution to my problem and its still kinda strange. There was actually two problems;
- for some strange reason I cant declare my RegEx string using a literal (using @ when assiging the string) if the literal spans multiple lines, for some reason this is messing with my RegEx. Which is why i guess in your example its working.
- the emails are being retrieved from Exchange via WebDAV and for some strange reason line breaks are only coming back as '\n' not '\r\n' like they should. So if I do a .Replace("\n","\r\n") on the body everything works fine. I guess this wasnt a problem for your in your example because the email body was assigned as a literal and therefore had the "proper" line breaks (\r\n)
Thanks again for your help guys, i never would have found this without your help.
If at first you don't succeed ... post it on The Code Project and Pray.
-
Hello D@nish, First of all i would like to thank both you and Riced. I have found the solution to my problem and its still kinda strange. There was actually two problems;
- for some strange reason I cant declare my RegEx string using a literal (using @ when assiging the string) if the literal spans multiple lines, for some reason this is messing with my RegEx. Which is why i guess in your example its working.
- the emails are being retrieved from Exchange via WebDAV and for some strange reason line breaks are only coming back as '\n' not '\r\n' like they should. So if I do a .Replace("\n","\r\n") on the body everything works fine. I guess this wasnt a problem for your in your example because the email body was assigned as a literal and therefore had the "proper" line breaks (\r\n)
Thanks again for your help guys, i never would have found this without your help.
If at first you don't succeed ... post it on The Code Project and Pray.
Instead of doing a string.Replace, you could also change
(?:\r\n)+
to[\r\n]+
, which should match any kind of newline