3

I'm looking for a Regex for European name. I found this one, works good with javascript:

/^([ \u00c0-\u01ffa-zA-Z'\-])+$/

I convert it for PHP, but it doesn't work:

/^([ \x{00C0}-\x{01FF}a-zA-Z'\-])+$/

My regex must match with name like that:

Jean-Paul le Marchant

Jérôme L'activé

So characters a-zA-Z, no special chars like @#$%^© etc. but all accented/european characters like éèàôç etc.

Community
  • 1
  • 1
alexmngn
  • 9,107
  • 19
  • 70
  • 130
  • I suppose you also need `'` even though it is a "special character"? – djechlin Nov 27 '12 at 15:26
  • Why would you do this? Maybe you can prevent someone from entering 'J0hn', but they still can enter a name like 'oooooooooooooooooooooooooooooo'. You're never going to stop malicious input with a regex check. And since you apparently support unicode already, don't worry about anything anyone enters. Maybe someone *is* called 'John Doe the 3rd' or 也许明年. Why block it? (Sorry if I've offended any Chinese people.) – GolezTrol Nov 27 '12 at 16:17

3 Answers3

5

You need to activate Unicode mode for these escape sequences to work:

/^([ \x{00C0}-\x{01FF}a-zA-Z'\-])+$/u

Note that the parentheses are not necessary:

/^[ \x{00C0}-\x{01FF}a-zA-Z'\-]+$/u

A character class gives a single "element" of your regex that can be repeate with a quantifier immediately.

Martin Ender
  • 43,427
  • 11
  • 90
  • 130
0

Make sure you use utf8 encoding and then slap the u modifier on the regex:

$regex = '/^([ \x{00C0}-\x{01FF}a-zA-Z\'\-])+$/u';
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

This PHP regex should work:

$str = "Jérôme L'activé";
if (preg_match("/^[\s\x{00C0}-\x{01FF}a-z'-]+$/iu", $str, $match))
   print_r($match);

OUTPUT:

Array
(
    [0] => Jérôme L'activé
)

RegEx Flags

  • u - Enable Unicode in matching
  • i - Ignore Case Comparison

PS: Inside square brackets you don't need to escape hyphen - if it is at first or last place.

anubhava
  • 761,203
  • 64
  • 569
  • 643