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

  • SEARCH
  • Home
  • 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 301649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:01:31+00:00 2026-05-12T07:01:31+00:00

This seems to match the rules I have defined, but I only starting learning

  • 0

This seems to match the rules I have defined, but I only starting learning regex tonight, so I am wondering if it is correct.

Rules:

  • Usernames can consist of lowercase
    and capitals
  • Usernames can consist of alphanumeric
    characters
  • Usernames can consist of underscore
    and hyphens and spaces
  • Cannot be two underscores, two hypens
    or two spaces in a row
  • Cannot have a underscore, hypen or
    space at the start or end

Regex pattern:

/^[a-zA-Z0-9]+([a-zA-Z0-9](_|-| )[a-zA-Z0-9])*[a-zA-Z0-9]+$/
  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-12T07:01:31+00:00Added an answer on May 12, 2026 at 7:01 am

    The specs in the question aren’t very clear, so I’ll just assume the string can contain only ASCII letters and digits, with hyphens, underscores and spaces as internal separators. The meat of the problem is insuring that the first and last character are not separators, and that there’s never more than one separator in a row (that part seems clear, anyway). Here’s the simplest way:

    /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/
    

    After matching one or more alphanumeric characters, if there’s a separator it must be followed by one or more alphanumerics; repeat as needed.

    Let’s look at regexes from some of the other answers.

    /^[[:alnum:]]+(?:[-_ ]?[[:alnum:]]+)*$/
    

    This is effectively the same (assuming your regex flavor supports the POSIX character-class notation), but why make the separator optional? The only reason you’d be in that part of the regex in the first place is if there’s a separator or some other, invalid character.

    /^[a-zA-Z0-9]+([_\s\-]?[a-zA-Z0-9])*$/
    

    On the other hand, this only works because the separator is optional. After the first separator, it can only match one alphanumeric at a time. To match more, it has to keep repeating the whole group: zero separators followed by one alphanumeric, over and over. If the second [a-zA-Z0-9] were followed by a plus sign, it could find a match by a much more direct route.

    /^[a-zA-Z0-9][a-zA-Z0-9_\s\-]*[a-zA-Z0-9](?<![_\s\-]{2,}.*)$/
    

    This uses unbounded lookbehind, which is a very rare feature, but you can use a lookahead to the same effect:

    /^(?!.*[_\s-]{2,})[a-zA-Z0-9][a-zA-Z0-9_\s\-]*[a-zA-Z0-9]$/
    

    This performs essentially a separate search for two consecutive separators, and fails the match if it finds one. The main body then only needs to make sure all the characters are alphanumerics or separators, with the first and last being alphanumerics. Since those two are required, the name must be at least two characters long.

    /^[a-zA-Z0-9]+([a-zA-Z0-9](_|-| )[a-zA-Z0-9])*[a-zA-Z0-9]+$/
    

    This is your own regex, and it requires the string to start and end with two alphanumeric characters, and if there are two separators within the string, there have to be exactly two alphanumerics between them. So ab, ab-cd and ab-cd-ef will match, but a, a-b and a-b-c won’t.

    Also, as some of the commenters have pointed out, the (_|-| ) in your regex should be [-_ ]. That part’s not incorrect, but if you have a choice between an alternation and a character class, you should always go with the character class: they’re more efficient as well as more readable.

    Again, I’m not worried about whether “alphanumeric” is supposed to include non-ASCII characters, or the exact meaning of “space”, just how to enforce a policy of non-contiguous internal separators with a regex.

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

Sidebar

Related Questions

I want to match strings like this: !! so I suppose the input have
Note: This is a follow up to this question . I have a legacy
In my IIS 7 configuration, I have created friendly URLs to convert: http://mysite/restaurant.aspx?Name=SomeName to
I tried this, var rows = []; for (var i = 0; i <
In my listview object insert/edit template, I have two dropdownlists (category, subcategory). When a
Link to the site in question . You can look at the source for
I wonder why the name after the #ifndef directive is always all caps and
Usually when the Page_Load event handler is added to a codebehind file by Visual
I'm working on a multilingual application that uses IIS7-based url rewriting. I'd like the
I'm working on a small Python script to clean up HTML documents. It works

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.