I try to validate names but I don't know if it is appropriate to use this expression
[A-ZÄËÏÖÜÁÉÍÓÚÂÊÎÔÛÀÈÌÒÙ]
[a-zäÄëËïÏöÖüÜáéíóúáéíóúÁÉÍÓÚÂÊÎÔÛâêîôûàèìòùÀÈÌÒÙ]
+(\s)*
[a-zäÄëËïÏöÖüÜáéíóúáéíóúÁÉÍÓÚÂÊÎÔÛâêîôûàèìòùÀÈÌÒÙ]+
What I want to validate is for example:
- Mary of the Cross
- Jose Antonio
In my regular expression when I get to the part (\s)* then I want to repeat the same process again without writing the first thing, I don't know if you understand me. An example of what I want to do would be something like this:
[A-ZÄËÏÖÜÁÉÍÓÚÂÊÎÔÛÀÈÌÒÙ]
[a-zäÄëËïÏöÖüÜáéíóúáéíóúÁÉÍÓÚÂÊÎÔÛâêîôûàèìòùÀÈÌÒÙ]
+(\s)*
([a-zäÄëËïÏöÖüÜáéíóúáéíóúÁÉÍÓÚÂÊÎÔÛâêîôûàèìòùÀÈÌÒÙ]
+(\s)*)*
([a-zäÄëËïÏöÖüÜáéíóúáéíóúÁÉÍÓÚÂÊÎÔÛâêîôûàèìòùÀÈÌÒÙ]
+(\s)*)*
My answer is based on the more general question of your question:
When you want to repeat the same expression/block with some separator between the pieces, the following is usually used:
Where:
For example, in your case it could have been something like this (it will be a bit different because you want the first one to be capitalized)
Where:
Also, it's probably a good idea to group all of this between start and end of line
^$
to avoid finding "Uan perez" in "jUan perez"Alternative:
As you will see, although the previous method is the most common, it repeats the expression (only) once.
There are other alternatives to not repeat the expression, such as the one I present below, although it has the drawback that you will not have control over the number of repetitions (number of names and surnames). I also leave it as a curiosity or in case it can be useful to you.
It would consist of:
So, for the case of names that start in capital letters, it would be something like this (this time, for simplicity I will use SEPARATOR = space and Allowed characters =
a-z
)^(?!.* (?: |$))[A-Z][a-z ]+$
If you don't care if there are two spaces in a row, this would be easier
Example:
^(?!.* $)[A-Z][a-z ]+$
Alternative with variables:
Of course, remember that in the vast majority of programming languages, you can store chunks of regular expressions in a variable and then use the variables within the regular expression, so that you can repeat the same chunks effortlessly.