When creating a regular expression, you need to keep in mind what you want to give match. I learned a lot about this from Aurelius Verde’s book Regular Expressions; the regular expression guide is made available free of charge.
I will divide the construction of this regular expression in 2 steps:
- Match a name without special characters or numbers (ASCII)
- Match on multiple names
(I will use the nomenclature of the regex guide of the Green Aurelium)
To match a name, I need to know which characters may be in the name. In this case, all letters (uppercase and lowercase), at least once. To match the letters, we can use a list containing those letters: [A-Za-z]
. So I can guarantee the letters. To guarantee a name, a letter needs to appear once or more times, so it needs to be the plus, staying like this now: [A-Za-z]+
The interpretation you get when reading this regular expression is: alphabetic characters, in high or low box, that appear once or more times. That doesn’t include multiple words yet.
To include another name, I need to separate the next name from the previous one with space, and it needs to have 1 or more letters that make up a name. So I can say that subsequent names are given by regular expression [A-Za-z]+
(note the space before the list). This expression is interpreted as a space followed by a name (as described above). I can use the group followed by asterisk, because secondary names may appear once or more times. So, ( [A-za-z]+)*
represents all names after the first name. To put a name before that, we obtain [A-za-z]+( [A-za-z]+)*
.
A weaker check (which allows multiple spaces between names, to begin with space or to consist exclusively of spaces) is [ A-Za-z]+
. The interpretation of this expression is: a string of alphabetic characters, independent of case, and blank spaces.
regex only allows letters (uppercase and minuscule), you can add a space in the list like this
[a-zA-Z ]
or[a-zA-Z\s]
– rray
I haven’t yet managed the two ways you quoted.
– user67378