Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:41:37+00:00 2026-05-10T20:41:37+00:00

I have a regular expression to match a persons name. So far I have

  • 0

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

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:41:38+00:00Added an answer on May 10, 2026 at 8:41 pm

    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’ )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a regular expression handy that will match any legal DNS hostname
What follows is a regular expression I have written to match multi-line pre-processor macros
I have a regular expression like this (much simplified): ^(ab)*$ And am matching against
I have a regular expression to match 2 different number formats: \=(?[0-9]+)\?|\+(?[0-9]+)\? This should
I have a regular expression to match anything between { and } in my
I have a syntax highlighting function in vb.net. I use regular expressions to match
I have this regular expression: ^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$ however it is failing when i have an
I have the following regular expression : I figured out most of the part
I have a Python regular expression that contains a group which can occur zero
I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.