I have a regular expression to match a persons name.
So far I have ^([a-zA-Z\’\s]+)$ but id like to add a check to allow for a maximum of 4 spaces. How do I amend it to do this?
Edit: what i meant was 4 spaces anywhere in the string
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Screw the regex.
Using a regex here seems to be creating a problem for a solution instead of just solving a problem.
This task should be ‘easy’ for even a novice programmer, and the novel idea of regex has polluted our minds!.
ROCKET SCIENCE.
replies
No. Php, toyed a bit with ruby, now going manically into perl.
There are some thing ( like this case ) where the regex based alternative is computationally and logically exponentially overly complex for the task.
I’ve parse entire php source files with regex, I’m not exactly a novice in their use.
But there are many cases, such as this, where you’re employing a logging company to prune your rose bush.
I could do all steps 2 to 5 with regex of course, but they would be simple and atomic regex, with no weird backtracking syntax or potential for recursive searching.
The steps 1 to 5 I list above have a known scope, known range of input, and there’s no ambiguity to how it functions. As to your regex, the fact you have to get contributions of others to write something so simple is proving the point.
I see somebody marked my post as offensive, I am somewhat unhappy I can’t mark this fact as offensive to me. 😉
Proof Of Pudding:
If there is anything ambiguous to anybody how that works, I’ll be glad to explain it to them. Noted that I’m still doing it with regexps. Other languages I would have used their native ‘trim’ functions provided where possible.
Bollocks –>
I first tried this approach. This is your brain on regex. Kids, don’t do regex.
This might be a good start
( Linebroken for clarity )
( Actual )
I’ve used
[^\s]+here instead of your A-Z combo for succintness, but the point is here the nested optional groupsie:
( Note: this, while being convoluted, has the benefit that it will match each name into its own group )
If you want readable code:
( it anchors around the (capture|) mantra of ‘get this, or get nothing’ )