#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    73
    Rep Power
    9

    Full Name String Match


    Hello All,

    I've been working on a regex problem and about to tear my hair out. I know I'm missing something small and have tried many different type patterns, using a perl compat reg ex, I want to match a fullname column and take into account that some last names have spaces or a single quote (') in it.

    So, I'm looking for a pattern that would match the following:

    Bob O'Malley
    Bob De Temple
    Bob DeTemple

    I can come up with patterns that will match the above but also seems to match the below which I don't want

    Bob 0'm8lly
    Bob De T3mple
    Bob DeT3mple

    One that has done what I've previously mentioned is /(\w+)\s?(\w+)/

    I realize it's matching the second set of strings as pattern matches more than once ,any help would be most appreciated
    Last edited by bsd-n-tux-rules; September 11th, 2008 at 11:11 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2008
    Location
    Plymouth UK
    Posts
    956
    Rep Power
    155
    Originally Posted by bsd-n-tux-rules
    Hello All,

    I've been working on a regex problem and about to tear my hair out. I know I'm missing something small and have tried many different type patterns, using a perl compat reg ex, I want to match a fullname column and take into account that some last names have spaces or a single quote (') in it.

    So, I'm looking for a pattern that would match the following:

    Bob O'Malley
    Bob De Temple
    Bob DeTemple

    I can come up with patterns that will match the above but also seems to match the below which I don't want

    Bob 0'm8lly
    Bob De T3mple
    Bob DeT3mple

    One that has done what I've previously mentioned is /(\w+)\s?(\w+)/

    I realize it's matching the second set of strings as pattern matches more than once ,any help would be most appreciated

    Arrrggghhhhh Now i remember why i HATE HATE HATE regex's lol

    Right.. i have something that seems to be working.. but it's a mosh up of another regex i was using before (i really suck @ regex)... i can make it only include the characters you want to allow.. but i can't make it check there's a space in there somewhere without breaking it :@

    This any use as an example?

    PHP Code:
    <?
    $name 
    = array(
    "Bob O'Malley",
    "Bob De Temple",
    "Bob DeTemple",
    "Bob 0'm8lly",
    "Bob De T3mple",
    "Bob DeT3mple",
    "BobDeTemple"
    );

    foreach(
    $name as $aname){
        if(
    preg_match("/^([a-zA-Z' ]+)$/",$aname))
            
    $output .= $aname." | ";
    }

    echo 
    "Matches:<br />
    $output";
    ?>
    If you do solve it. pls tell me how.. I will get to grips with regex one day lol
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2008
    Location
    Plymouth UK
    Posts
    956
    Rep Power
    155
    Ahhhh hahahahahahahahahaha
    FINALLY
    I was going about it completely the wrong way..... Sussed it...

    PHP Code:
    <?
    $name 
    = array(
    "Bob O'Malley",
    "Bob De Temple",
    "Bob DeTemple",
    "Bob 0'm8lly",
    "Bob De T3mple",
    "Bob DeT3mple",
    "BobDeTemple",
    "Bob De-Temple"
    );

    foreach(
    $name as $aname){
        
    //if(preg_match("/^([a-zA-Z' ]+)$/",$aname))
        
    if(preg_match("/^[A-Z][a-zA-Z']+[ ]+[A-Z][a-zA-Z'\- ]*$/",$aname))
            
    $output .= $aname." | ";
    }

    echo 
    "Matches:<br />
    $output";
    ?>
    Hope that's what you were after
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    73
    Rep Power
    9
    Yes, thank you!

    However, cuz this is for a webform submission field check, I had to make it a bit more case insensitive by dropping the [A-Z] but without your help, I'd still be pulling my hair out!
    PHP Code:
     $name = array( 
    "Bob O'Malley"
    "Bob De Temple"
    "Bob DeTemple"
    "Bob 0'm8lly"
    "Bob De T3mple"
    "Bob DeT3mple"
    "BobDeTemple"
    "Bob De-Temple",
    "Bob O Mally",
    "bob de temple" 
    ); 

    foreach(
    $name as $aname){ 
        
    //if(preg_match("/^([a-zA-Z' ]+)$/",$aname)) 
        
    if(preg_match("/^[a-zA-Z']+[ ]+[a-zA-Z'\- ]*$/",$aname)) 
            
    $output .= $aname." | "


    echo 
    "Matches:<br /> 
    $output"
    ?> 

    I swear I had some pattern fairly close to yours but like you, I HATE REGEX too. But also realize, it's very cool so I love it too.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Sorry guys, but your regexp fails on hyphenated first names, like Anne-Marie.

    It also allows for an entire sentence to be typed, as long as you continue to use the correct characters. Your regexp will match "The quick brown fox jumped over the lazy dog" and miss "anne-marie smith".

    Try:
    PHP Code:
    <?
    $name 
    = array( 
    "Bob O'Malley"
    "Bob De Temple"
    "Bob DeTemple"
    "Bob 0'm8lly"
    "Bob De T3mple"
    "Bob DeT3mple"
    "BobDeTemple"
    "Bob De-Temple",
    "Bob O Mally",
    "bob de temple" ,
    "Anne-Marie smith",
    "The quick brown fox jumped over the lazy dog",
    "John Smith",
    "Jonny-boy o'Henry-james III",
    ); 

    foreach(
    $name as $aname){ 
        if(
    preg_match("/^([a-zA-Z'-]+\s*){2,5}$/",$aname)) 
            echo 
    "Matches: $aname\n";

    ?>
    Note the {2,5} near the end of my regexp. That indicates that there is a minimum of 2 "name parts" and a maximum of 5 "name parts". "Jonny-boy o'Henry-james III" has 3 name parts. something like "William Patrick Henry O Mally III" would fail to match, as that is a 6 part name, despite being a valid Irish Catholic name. Also, names like Madonna will clearly fail to match, as they're only 1 name part.

    -Dan

    Comments on this post

    • mikeyskona agrees : Damn. Cant rep you. You deserve rep for that :D lol I hate regex :|
    • prometheuzz agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    73
    Rep Power
    9
    Awesome! Thanks...I also tried using a min,max too but I was very frustrated...still am as you can get that to work and I couldn't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Regular Expression for full name


    This isn't quite right because of the \s* means that the space can be 0 or more times. You require the space on the string to indicate a different name part.

    This is correct:
    PHP Code:
    <?
    $name 
    = array(
    "Bob",
    "Bob2",
    "Bob ",
    "Bob Bob ",
    "Bob O'Malley",
    "Bob De Temple",
    "Bob DeTemple",
    "Bob 0'm8lly",
    "Bob De T3mple",
    "Bob DeT3mple",
    "BobDeTemple",
    "Bob De-Temple",
    "Bob O Mally",
    "bob de temple" ,
    "Anne-Marie smith",
    "The quick brown fox jumped over the lazy dog",
    "John Smith",
    "Jonny-boy o'Henry-james III",
    );

    foreach(
    $name as $aname){
        if(
    preg_match("/^([a-zA-Z'-]+\s+){1,4}[a-zA-z'-]+$/",$aname))
            echo 
    "Matches: |$aname|\n";
    }
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo