Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. RegEx not finding matches ... but it should

RegEx not finding matches ... but it should

Scheduled Pinned Locked Moved C#
regexcsharpsaleshelpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam R Harris
    wrote on last edited by
    #1

    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.

    D R 2 Replies Last reply
    0
    • A Adam R Harris

      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.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      If you can provide some sample text that this Regex should match, it might help.

      A 1 Reply Last reply
      0
      • D dan sh

        If you can provide some sample text that this Regex should match, it might help.

        A Offline
        A Offline
        Adam R Harris
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • A Adam R Harris

          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.

          R Offline
          R Offline
          riced
          wrote on last edited by
          #4

          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 doing myRE = "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

          A 1 Reply Last reply
          0
          • R riced

            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 doing myRE = "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

            A Offline
            A Offline
            Adam R Harris
            wrote on last edited by
            #5

            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.

            R 1 Reply Last reply
            0
            • A Adam R Harris

              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.

              R Offline
              R Offline
              riced
              wrote on last edited by
              #6

              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

              A 1 Reply Last reply
              0
              • A Adam R Harris

                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

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #7

                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: 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";

                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)+");

                A 1 Reply Last reply
                0
                • R riced

                  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

                  A Offline
                  A Offline
                  Adam R Harris
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • D dan sh

                    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: 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";

                    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)+");

                    A Offline
                    A Offline
                    Adam R Harris
                    wrote on last edited by
                    #9

                    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;

                    1. 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.
                    2. 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.

                    D G 2 Replies Last reply
                    0
                    • A Adam R Harris

                      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;

                      1. 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.
                      2. 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.

                      D Offline
                      D Offline
                      dan sh
                      wrote on last edited by
                      #10

                      Great!!! :-D

                      1 Reply Last reply
                      0
                      • A Adam R Harris

                        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;

                        1. 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.
                        2. 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.

                        G Offline
                        G Offline
                        Gideon Engelberth
                        wrote on last edited by
                        #11

                        Instead of doing a string.Replace, you could also change (?:\r\n)+ to [\r\n]+, which should match any kind of newline

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups