RegEx not finding matches ... but it should
-
*************************** For the record this is a cross-post. I dont want to get anyone mad it's just that i really need help with this Original Post[^] *************************** 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. -
*************************** For the record this is a cross-post. I dont want to get anyone mad it's just that i really need help with this Original Post[^] *************************** 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.Hi, I would debug such beast using "binary search", i.e. throw away half of the search specification, and keep doing that until something matches. Then you know the problem is in the part you last removed. OTOH I would never come up with such a complex Regex pattern in the first place. It is just unreadable. And what happens if someone somewhere decides to add/delete a space, a new line, whatever to the e-mail format? IMO you need several smaller operations to extract the fields individually, your code looks like a maintenance nightmare to me. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, I would debug such beast using "binary search", i.e. throw away half of the search specification, and keep doing that until something matches. Then you know the problem is in the part you last removed. OTOH I would never come up with such a complex Regex pattern in the first place. It is just unreadable. And what happens if someone somewhere decides to add/delete a space, a new line, whatever to the e-mail format? IMO you need several smaller operations to extract the fields individually, your code looks like a maintenance nightmare to me. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Hello Luc, Normally i would agree with you 200%, however this is a subcontract project which is going to be used by other developers. They wanted me to allow for configuration of the email parsing by having a setting which contains the RegEx to use. You can find the solution to my problem here[^], i didn't want to keep cross posting. Thanks again for your input.
If at first you don't succeed ... post it on The Code Project and Pray.
-
Hello Luc, Normally i would agree with you 200%, however this is a subcontract project which is going to be used by other developers. They wanted me to allow for configuration of the email parsing by having a setting which contains the RegEx to use. You can find the solution to my problem here[^], i didn't want to keep cross posting. Thanks again for your input.
If at first you don't succeed ... post it on The Code Project and Pray.
That's interesting. Thanks. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.