-3

I'm trying to create a good regular expression for people's Surname.

It should be valid if a Surname is:

abcd

abcd'efg

abcd-efg

abcd, .efg

etc...

I also need to test if symbols do not repeat... so for example:

abcd''efg

abcd-',

Are invalid but the one:

abcd, .efg

Can be valid.

At the moment I just created this:

^[a-z .',-_]+$

And now I'm trying to check for all the double symbols but I cannot go ahead successfully.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Ayeye Brazo
  • 3,316
  • 7
  • 34
  • 67

2 Answers2

3

It's a bad idea. There is no international list of allowed characters that people could use in their names. Some surnames even contain Unicode symbols — it will not be possible to write a regex that would perfectly validate all of them correctly. Even if you can come up with a regex, it might be too generic that it wouldn't be effective.

Read this article for why you shouldn't be doing this: Falsehoods Programmers Believe About Names

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I just need to create the regex I wrote above... Even if it is a bad idea. Thanks – Ayeye Brazo Aug 12 '14 at 13:30
  • I'll just quote [John](http://stackoverflow.com/a/888902/1438393): "*Step back and ask yourself what you are actually trying to accomplish. Then try to accomplish it without making any assumptions about what people's surnames are, or what they mean.*". – Amal Murali Aug 12 '14 at 13:31
2

If after reading this insightful post by Amal Murali and you still want to do this with a regex, please see this:

/^(?![^'\-_\n]*['\-_][^'\-_\n]*['\-_])[a-z .',-_]+$/m

View a regex demo!

Community
  • 1
  • 1
Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • And this doesn't match `abcd"efg` as valid (single double quote that is). Moreover `And now I'm trying to check for all the double symbols but I cannot go ahead successfully.` is matched using this regex as valid surname. – anubhava Aug 12 '14 at 14:20