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 8146063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:52:02+00:00 2026-06-06T13:52:02+00:00

Is this Regex correct if I have to match a string which is atleast

  • 0

Is this Regex correct if I have to match a string which is atleast 7 characters long, not more than 20 characters, has atleast 1 number, and atleast 1 letter? It has no other constraints.

[0-9]+[A-Za-z]+{7,20}

Thanks

  • 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-06-06T13:52:04+00:00Added an answer on June 6, 2026 at 1:52 pm

    No, it’s not. The quantifier {7,20} doesn’t apply to a token (repetition in regexes is done with quantifiers, like *, +, ? or the more general {n,m} – you cannot use more than one quantifier on a single token [in this case [a-zA-Z]]; *? is a quantifier on its own and thus doesn’t play by above rules). You’ll need something like the following:

    ^(?=.*\d)(?=.*[a-zA-Z]).{7,20}$
    

    This has two lookaheads making sure of at least one digit and at least one letter:

    (?=.*\d)
    (?=.*[a-zA-Z])
    

    Lookarounds are zero-width assertions; they do not consume characters in the string so they are merely matching a position. But they make sure that the expression inside of them would match at the current point. In this case this expression would match arbitrarily many characters and then would require a digit or a letter, respectively.

    The actual match itself,

    .{7,20}
    

    just makes sure the length matches. What characters are used is irrelevant because we made sure of that constraints above already.

    Finally the whole expression is anchored in that a start-of-string and end-of-string anchor are inserted at the start and end:

    ^...$
    

    This makes sure that the match really encompasses the whole string. While not strictly necessary in this case (it would match the whole string anyway in all valid cases) it’s often a good idea to include because usually regexes match only substrings and this can lead to subtle problems where validation regexes match even though they should fail. E.g. using \d+ to make sure a string consists only of digits would match the string a4b which puzzles beginners quite often.

    I also changed that the order of letters and numbers doesn’t matter. Your regex looks like it tries to impose a definite order where all numbers need to come before all letters which usually isn’t what’s wanted here.

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

Sidebar

Related Questions

I have this regex $folder =~ / (?!\d{3}) /xmi which I use to match
I have this regex in PHP: preg_match('/\[summary\](.+)\[\/summary\]/i', $data['text'], $match); It works fine when the
I have this code below which checks whether the user has entered a syntactically
Consider this regex: ^([0-9]+)(?: ([-\+/%\*]) ([0-9]+))+$ In english, it says a number, followed by
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can
I have this regex working but now need to allow numbers without the decimal
I've created this regex (www|http://)[^ ]+ that match every http://... or www.... but I
I have this regex thanks to another wonderful StackOverflow user /(?:-\d+)*/g I want it
I have this homework problem where I need to use regex to remove every
i have strings in the form [abc].[some other string].[can.also.contain.periods].[our match] i now want to

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.