Marian Rick
Marian Rick

Reputation: 3430

How to check regex string space string?

I know that there are a lot of different regex questions out there, but I cannot find a matching one:

I need to validate an input field, which should contain a string + space + string. Inside the strings, there should all characters be allowed (ö,ä,ü,á,é).

Valid examples:

a b

George Michael

This is gööd

Invalid examples:

George

Michael

GeorgeMichael

So all in all, I want to check wether a person has written its firstname only or both, first- and lastname. I appreciate any suggestions.

Upvotes: 1

Views: 72

Answers (2)

Toto
Toto

Reputation: 91415

If you're using a language that allows unicode properties (perl, php, python, ...), you could do:

\p{L}+\s+\p{L}+

That will matches two words in any lang separated by one or more sapces.

If you could have more than 2 words but at least two:

\p{L}+\s+\p{L}+(?:\s+\p{L}+)*

Where \p{L} stands for any letter in any language.

Upvotes: 1

Nabeel Khan
Nabeel Khan

Reputation: 3993

This regex should help you confirm that the person is using first and last name both (2 names, not three or more).

Updated:

This now matches third word too optionally

\S+ \S+(?: \S+)?

Working example here: https://regex101.com/r/Tpo8gy/3

Upvotes: 1

Related Questions