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. Problem matching name using Regex

Problem matching name using Regex

Scheduled Pinned Locked Moved C#
regexcsharphelpquestionlounge
7 Posts 5 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.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using: \b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b ..and here is the string I`m matching: "JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;" Like I said, in Expresso it matches the full name, but when I use: MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); or Match m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); it doesn't return any matches. Anything I could be doing wrong?

    D OriginalGriffO L 3 Replies Last reply
    0
    • E Etienne_123

      Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using: \b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b ..and here is the string I`m matching: "JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;" Like I said, in Expresso it matches the full name, but when I use: MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); or Match m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); it doesn't return any matches. Anything I could be doing wrong?

      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      Are you missing parentheses around your character class for upper and lower-case letters? I think [a-z][A-Z]* in both places should be replaced with ([a-z][A-Z])+. P.S. There is a separate forum for regular expressions[^].

      1 Reply Last reply
      0
      • E Etienne_123

        Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using: \b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b ..and here is the string I`m matching: "JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;" Like I said, in Expresso it matches the full name, but when I use: MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); or Match m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); it doesn't return any matches. Anything I could be doing wrong?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Firstly, we do have a Regex forum: http://www.codeproject.com/Forums/1580841/Regular-Expressions.aspx[^] - it might be more appropriate in future. Secondly, in Expresso, go to the Design tab and look at the bottom: You will find you have "Ignore case" selected. If you de-select that, Expresso will not find it either. Change your string to:

        \b[a-zA-Z][a-zA-Z]*\b\s*\b[a-zA-Z][a-zA-Z]*\b

        And it will work in both, or use the RegexOption.IgnoreCase as part of the Match method. MSDN[^]

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        E 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Firstly, we do have a Regex forum: http://www.codeproject.com/Forums/1580841/Regular-Expressions.aspx[^] - it might be more appropriate in future. Secondly, in Expresso, go to the Design tab and look at the bottom: You will find you have "Ignore case" selected. If you de-select that, Expresso will not find it either. Change your string to:

          \b[a-zA-Z][a-zA-Z]*\b\s*\b[a-zA-Z][a-zA-Z]*\b

          And it will work in both, or use the RegexOption.IgnoreCase as part of the Match method. MSDN[^]

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

          E Offline
          E Offline
          Etienne_123
          wrote on last edited by
          #4

          Thanks I only noticed that now.

          1 Reply Last reply
          0
          • E Etienne_123

            Hi I seem to be having a problem matching names using Regex, even though it works perfectly in Expresso. The goal is to match only the name in the string below. Here is the Regex I`m using: \b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b ..and here is the string I`m matching: "JOHN DOE john@random.net(H) 04377777746, (W) 0444444543, (F) 022222223, (M) 082343222;" Like I said, in Expresso it matches the full name, but when I use: MatchCollection NameCollectionRegex = Regex.Matches(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); or Match m = Regex.Match(contactDetails, @"(\b[a-z][A-Z]*\b\s*\b[a-z][A-Z]*\b)"); it doesn't return any matches. Anything I could be doing wrong?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Griff's answer seems appropriate for the question you asked, however your question may be a bit shortsighted, as names can be a bit more complex than you are expecting. Here are a few CP member names you would still have trouble with:

            wout de zeeuw
            Pete O'Hanlon
            Smithers-Jones
            Ennis Ray Lynch, Jr.

            What I would do is locate the first digit or @ sign, then get everything that sits before the last space in front of that. And no, I would not use regex to implement that, it is way simpler using two methods from the string class. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            P 1 Reply Last reply
            0
            • L Luc Pattyn

              Griff's answer seems appropriate for the question you asked, however your question may be a bit shortsighted, as names can be a bit more complex than you are expecting. Here are a few CP member names you would still have trouble with:

              wout de zeeuw
              Pete O'Hanlon
              Smithers-Jones
              Ennis Ray Lynch, Jr.

              What I would do is locate the first digit or @ sign, then get everything that sits before the last space in front of that. And no, I would not use regex to implement that, it is way simpler using two methods from the string class. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              What I would do is locate the first digit ...

              Perhaps you aren't familiar with Tom Lehrer's friend Hen3ry? :-D

              L 1 Reply Last reply
              0
              • P PIEBALDconsult

                Luc Pattyn wrote:

                What I would do is locate the first digit ...

                Perhaps you aren't familiar with Tom Lehrer's friend Hen3ry? :-D

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Indeed I'm not. Plz send appropriate regex codez. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                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