C # regular expression for a name that includes interior space

I need a regular expression for C # that matches "Johnson", "Del Sol" or "Del La Range"; in other words, it must match words with spaces in the middle, but without a space at the beginning or end.

+5
source share
7 answers
^\p{L}+(\s+\p{L}+)*$

This regular expression has the following functions:

  • Matches one surname (e.g., surname Malcolm X)
  • Will not match names containing numbers (e.g., anything with \wor [^ ])
  • Matches Unicode Characters

But what about names like O'Connor or cheap names ... hmm ...

+5

:

^[a-zA-Z][a-zA-Z ]*[a-zA-Z]$

: , / :

^[a-zA-Z'][a-zA-Z'- ]*[a-zA-Z']?$
+3

"Ṣalāḥ ad-Dīn Yūsuf ibn Ayyūb" (. http://en.wikipedia.org/wiki/Saladdin), , ? " " ()? " -" (. http://en.wikipedia.org/wiki/Chang_Kai-shek)?

! . : .

+3

? - . . , :

^(.+?) (.+)$

1 , 2 .

, , , ?

0

- :

^[^\s][\w\s]*[^\s]$
0

:

/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/

, .

0

, , :

^[^ ][a-zA-Z ]+[^ ]$

, - , .

irb, , #, :

( , nil )

>> "Di Giorno" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> 0
>> "DiGiorno" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> 0
>> " DiGiorno" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> nil
>> "DiGiorno " =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> nil
>> "Di Gior no" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> 0
-1

All Articles