tags:

views:

40

answers:

2

Hey Guys,

Tried looking around for a regex pattern for a full name and just can't seem to find one.

Ideally would match like

Tom Franklin
tom Franklin
tom franklin
tom franklin-jones

i.e. allow 1 space in the middle and some basic hyphens etc but thats all. Does any one know how to do this ?

Edit: Including

René Hadron van der Ööps
A: 

\w+\s+[a-zA-Z-]+

GameBit
hi gamebit - thanks for the response. i'm using ascii and when I enter that - doesnt like \w and \s - gives me "unrecognized escape sequence"
Tom
+1  A: 

You can use /([\p{L}'-]+) ([\p{L}'-]+)/ to catch all possible letter. Or /([a-z'-]+) ([a-z'-]+)/i to catch only ascii letters.

The group ([\p{L}'-]+) means [\p{L}'-] repeated at least one time. And \p{L} means any unicode letter. The - at the end means that "-" is allowed in names.

Note : When you capture characters with [] the hyphen must be either at the start of your characters set or at the end.


Edit :

Mr. O'Brien is happy now.


Resources :

Colin Hebert
+1 for covering Unicode.
Daniel Vandersluis
thanks colin. yeah im using C# - doesnt like \w and \s - gives me "unrecognized escape sequence"
Tom
@Tom remember to escape the \ either by writing "\\s" or @"\s"
Arkain
It's not C# Tom - it's you. You must escape your backslashes or prefix the string with @ ie: string pattern = @"\w";
Goran
yep sorry - forgot - been a while :) thx
Tom
In this code you can't really detect "René Hadron van der Ööps" because you don't really know what is his firstname or lastname, plus I didn't use the \s, I suggest you to use it for the lastname.
Colin Hebert
err i seem to be getting a bug with the space ? i.e. seems even it i enter "tom jones" - still gives me my errore text "space is required"
Tom
What regex did you use ? It worked for me with `@"([\p{L}'-]+) ([\p{L}'-]+)"` on http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
Colin Hebert
cool sorry yeah - dodgy regex tester :)
Tom