<p>I have a regular expression to match a persons name.</p>

<p>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?</p>

<p><strong>Edit:</strong> what i meant was 4 spaces anywhere in the string</p>

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!.
```
```
1: Get Input    
2: Trim White Space
3: If this makes sence, trim out any 'bad' characters. 
4: Use the "split" utility provided by your language to break it into words
5: Return the first 5 Words.
```
```
ROCKET SCIENCE.
replies
what do you mean screw the regex? your obviously a VB programmer. 
  Regex is the most efficient way to work with strings. Learn them.
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:
```
```
sub getNames{
    my @args = @_;
    my $text = shift @args;
    my $num  = shift @args;

    # Trim Whitespace from Head/End
    $text =~ s/^\s*//;
    $text =~ s/\s*$//;

    # Trim Bad Characters (??)
    $text =~ s/[^a-zA-Z\'\s]//g;

    # Tokenise By Space 
    my @words = split( /\s+/, $text );

    #return 0..n 
    return @words[ 0 .. $num - 1 ];
} ## end sub getNames

print join ",", getNames " Hello world     this is a    good test", 5;
>> Hello,world,this,is,a
```
```
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
```
```
/([^\s]+
    (\s[^\s]+
      (\s[^\s]+
        (\s[^\s]+
          (\s[^\s]+|)
         |)
       |)
    |)
  )/
```
```
( Linebroken for clarity )
```
```
/([^\s]+(\s[^\s]+(\s[^\s]+(\s[^\s]+|)|)|))/
```
```
( Actual )
I've used
```
[^\s]+
```
here instead of your A-Z combo for succintness, but the point is here the nested optional groups
ie:
```
```
(Hello( this( is( example))))
(Hello( this( is( example( two)))))
(Hello( this( is( better( example))))) three
(Hello( this( is()))))
(Hello( this()))
(Hello())
```
```
( Note: this, while being convoluted, has the benefit that it will match each name into its own group )
If you want readable code:
```
```
$word = '[^\s]+'; 
  $regex = "/($word(\s$word(\s$word(\s$word(\s$word|)|)|)|)|)/";
```
```
( it anchors around the  (capture|)  mantra of "get this, or get nothing" )