2

I want a regular expression for full name with salutation. Can anyone please help me.

^[A-Za-z] ([A-Za-z] (\\s|\\.|_)?)+[a-zA-Z]*$

this is my regular expression which I'm using for full name but it's not taking salutation.

Spudley
  • 166,037
  • 39
  • 233
  • 307
yopirates
  • 153
  • 1
  • 3
  • 9
  • Might be better to explain a bit what your regexp do. I read "One letter followed by a space followed by one or many letters followed by a space followed by zero one space, period or underscore followed by zero or more characters. – Vincent Savard Nov 03 '10 at 11:30
  • this patten is taking without salutation.I need it with salutation.It takes like "ani a an" and "sow b.s" but i want it like "Dr.ani a an" – yopirates Nov 03 '10 at 11:34
  • @leppie I don know much about regex patterns.I just took this expression for validation purpose but it didnt solve my problem.My requirement is to get Dr.,Mr.,Mrs., in front of the name – yopirates Nov 03 '10 at 11:48
  • @yopirates: It was a joke. IMO it will be nigh impossible to get this 100% right. – leppie Nov 03 '10 at 11:50
  • cant we get a salutation in front of the name? – yopirates Nov 03 '10 at 11:55
  • 2
    You can't validate names with regexes, ever. For every attempt to do which isn't `/.*/` I can give you an valid name which fails, wanna bet? And we're not only talking whitespace, diacretics, ligatures and the like. – Wrikken Nov 03 '10 at 12:25
  • 1
    Sir Nigel Oliver St. John-Mollusc III., OBE will not like your site. – Tim Pietzcker Nov 03 '10 at 12:37
  • @Tim, neither shall Sr. Gabriel García-Márquez. – tchrist Nov 03 '10 at 12:50
  • 1
    @tchrist, @jensgram: Don't even get me started. Signed, Tarquin Fin-tim-lim-bim-lim-bin-bim-bin-bim bus stop F'tang F'tang Olé Biscuitbarrel (retired). – Tim Pietzcker Nov 03 '10 at 12:57

3 Answers3

6

Rule One: Never try to enforce rules on people's names. There will always be someone who you exclude, purely because their name doesn't match what you expect.

What about people who don't have (or want) a salutation? Or those who have more than one? "Professor Sir" is a perfectly valid combination in the UK, and in Germany it's common for someone with multiple degrees to call themselves "Doctor Doctor" or something similar.

And then there's the actual names. Your regex will fail even for relatively common western-style names like "Mary-Jane O'Brien" or "André van den Berg", let alone more unusual cases.

In short, it's virtually impossible to accurately validate a name field.

Here's a link to a page which describes some of the obvious (and not so obvious) things which people try to validate on names, which can trip you up:

http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

(I've posted a similar comment before here: How to "Validate" Human Names in CakePHP?)

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307
5

If you insist on doing this via a regexp, add (Dr|Mrs?|Ms)\. to the pattern. Will match:

  • Dr.
  • Mr.
  • Mrs.
  • Ms.

I.e., (given that you're satisfied with the rest of the regexp - taken directly from the question.)

^(Dr|Mrs?|Ms)\. [A-Za-z] ([A-Za-z] (\s|\.|_)?)+[a-zA-Z]*$

This, however, will not be sufficient to handle Sir Nigel Oliver St. John-Mollusc III., OBE (thanks, @Tim Pietzcker).


EDIT
(Dr|Mr?s?)\. was wrong, sorry. It would match M., too. Thanks, @tchrist.

jensgram
  • 31,109
  • 6
  • 81
  • 98
  • Hm, doesn’t that match "M. Frédéric Mitterrand", with the "M." presumably standing for *monsieur*? Oh wait, you spelled out `A-Z`. That’s almost always wrong. You need to use `\pL` if you mean any character that has the Unicode "letter" property. – tchrist Nov 03 '10 at 12:43
  • There is never a time to write single-character alternatives using pipes instead of creating a bracketed character class. Most regex compilers are too dumb to do this for you. – tchrist Nov 03 '10 at 12:45
  • How can I know? Is this data absolutely guaranteed to be nothing whatsoever but ASCII only? Seven-bit datastreams are so 1970s! – tchrist Nov 03 '10 at 12:48
  • @tchrist: because OP says so. – SilentGhost Nov 03 '10 at 12:49
  • @tchrist Correct about my `(Dr|Mr?s?)\. ` wrongfully matching `M.`. Regarding the rest: *I* wouldn't use it :) – jensgram Nov 03 '10 at 12:50
  • @SilentGhost: Wrong. They nowhere said they were stuck in ASCII. Just because their naïve pattern locked them into retro mode doesn’t make it right. They also included doubled slackbashes in a regex where they absolutely do not belong, which hardly inspires confidence in their grasp of the problem-space. – tchrist Nov 03 '10 at 12:54
2

You can use a slightly modified version of http://regexlib.com/REDetails.aspx?regexp_id=2502

madhurtanwani
  • 1,199
  • 7
  • 13