Helpful Information
 
 
Category: Regex Programming
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

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?


<?
$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 :D lol

Ahhhh hahahahahahahahahaha
FINALLY
I was going about it completely the wrong way..... Sussed it...


<?
$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 :)

Yes, thank you! :trockon:

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!


$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. :D

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:

<?
$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

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. :) :tntworth:

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:

<?
$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";
}
?>










privacy (GDPR)