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 marry one and only one character, among the possible options. Exchanging parentheses should solve the problem:
[A-Z][a-z]+([ ][A-Z][a-z]+)*
Note that, depending on how this expression is used, it can match only part of a string (eg, 123Fulano Beltrano456
would have its "middle" married). If you want to ensure that the expression only matches the entire string, one means is by using the start delimiters ( ^
) and end ( $
):
^[A-Z][a-z]+([ ][A-Z][a-z]+)*$
Finally, if you have problems with capture groups, mark the expression inside the parentheses as "do not capture":
^[A-Z][a-z]+(?:[ ][A-Z][a-z]+)*$
As for 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 (ie test the string by 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 Rubular . PS If you are using this regex within an XML, then perhaps the lookaheads are not available a>. I think that's not the case, but make sure the engine used supports this functionality. Otherwise, there is little I can suggest for you to validate the size, it would be ideal to do this in a separate step (such as suggested by Guill in comments ).
Note that some of your "valid" names are invalid by this regex - those that have "da" in the middle (started in lowercase). If you want to make an exception for "da" (and maybe also for "do", "de" and "and") you can do something like:
(?=^.{2,60}$)^[A-Z][a-z]+(?:[ ](?:das?|dos?|de|e|[A-Z][a-z]+))*$
Updated example .