Your general idea is ok (marry the first name, and zero or more times marry a space followed by another name), the problem is in the use of brackets ([]
) in the second part of the expression - brackets match one and only one character, among the possible options. Switching to parentheses should solve the problem:
[A-Z][a-z]+([ ][A-Z][a-z]+)*
Note that depending on how this expression is used, it can only marry part of a string (ex.: 123Fulano Beltrano456
would have your "middle" married). If you want to ensure that the expression only matches the entire string, a middle is using the start delimiters (^
) and end ($
):
^[A-Z][a-z]+([ ][A-Z][a-z]+)*$
Finally, if you have a problem with capture groups, mark the expression inside the parentheses as "do not capture":
^[A-Z][a-z]+(?:[ ][A-Z][a-z]+)*$
About validating by a specific size, that my answer in a related question ("2 regular expressions in 1") shows a way to do this using lookarounds (i.e. test the string for the first regex, without consuming it, then test it again for the second regex):
(?=^.{2,60}$)^[A-Z][a-z]+(?:[ ][A-Z][a-z]+)*$
Example in the Ruble. P.S. If you are using this regex inside an XML, then maybe the lookaheads are not available. I don’t think that’s the case, but make sure engine used supports this functionality. Otherwise, there is little that I can suggest for you to validate the size, the ideal would be to do this in a separate step (like suggested by Guill in the comments).
Note that some of their "valid" names are invalid by this regex - those that have "da" in the middle (lowercase started). If you want to make an exception for "da" (and maybe also for "do", "de" and "e") you can do something like:
(?=^.{2,60}$)^[A-Z][a-z]+(?:[ ](?:das?|dos?|de|e|[A-Z][a-z]+))*$
Updated example.
What language?
– gmsantos
Gmsantos .. C# / VB / . NET! Follows an expression that already works for another purpose. ([0-9a-za-Z]+([_. -]? [0-9a-za-Z]+)@[0-9a-za-Z]+[0-9,a-z,A-Z,.,-](.){1}[a-za-Z]{2,4})+$ This expression is placed inside an XML for validation of the answer in a question.
– Nycolas Merino
Can’t you perform any operation on the resulting string? A single line could either limit the number of characters or remove multiple spaces.
– Guill