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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:37:06+00:00 2026-05-23T15:37:06+00:00

OK so I have a mental block when it comes to regex – but

  • 0

OK so I have a mental block when it comes to regex – but I was told to come up with a regex expression that met these conditions:

  • must be at least 8 characters (easy!)
  • must have characters from at least 3 of the 4 different character types – upper case, lower case, digits, symbols (ok)
  • must have at least 5 different characters
  • must not have a long sequence of the same character type (eg. asdnme would be considered bad as its a long sequence of lower case)
(?=^.{8,255}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9\s])(?=.*[a-z])|(?=.*[^A-Za-z0-9\s])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9\s]))

This regex expression satisfies 1 and 2. But I am struggling to find examples for 3 and 4.

If any regex enthusiasts could help me – it would be appreciated. πŸ™‚

Note: I would prefer not to use Regex – this is me asking anyone if it’s possible to check for the 3rd and 4th condition using regex? And please don’t downvote me for the belief that regex is the only solution. I don’t believe it is – our achitect decided the least effort would be involved in using regex to solve this issue.

Personally I think this level of password security is going to make the system unusable!!! But maybe I don’t care enough about password security πŸ™‚

Note: We’re trying to make use of the Microsoft ASPNET Membership – regex expression. Which is why I thought it needed to be a single expression. I get that it’s horrible to try to read/understand.

If anyone can provide individual regex expressions for
– must have at least 5 different characters
– must not have a long sequence of the same character type (eg. asdnme would be considered bad as its a long sequence of lower case) – assume 5 sequence is too long..

Or c# code /javascript ? Although this is specific to one particular client – we don’t want it blanked applied to all clients. Which is probably why the architect wanted a nice regex expression that you could just slot in at deployment time. πŸ™

Found someone else’s example that works in .NET

^(?!.*(.)\1{2})((?[A-Z])|(?[a-z])|(?\d)|(?[^A-Za-z\d])){8,}(?(Upper)(?(Lower)(?(Numeric)|(?(NonAlphaNumeric)|(?!)))|(?(Numeric)(?(NonAlphaNumeric)|(?!))|(?!)))|(?(Lower)(?(Numeric)(?(NonAlphaNumeric)|(?!))|(?!))|(?!)))$

Unfortunately it meets these conditions:

  • Must have a minimum length of 8 characters

  • Must contain characters from three of the four following types:

  • English upper-case characters (A – Z)

  • English lower-case characters (a – z)

  • Numerical digits (0 – 9)

  • Non-alphanumeric characters

  • No character can be repeated 3 or more times in a row, e.g.
    BB (letter B twice) is OK, but BBB (letter B 3 times) is NOT OK.

But it doesn’t detect that at least 5 different characters are used πŸ™

Nevermind – the answer below seems to work. Only thing is that it appears to allow 4 different characters rather than requiring 5?

I have tweaked it to be:
^(?=.{8,})(?:(?=.\d)(?=.[A-Z])(?=.[a-z])|(?=.\d)(?=.[^A-Za-z0-9\s])(?=.[a-z])|(?=.[^A-Za-z0-9\s])(?=.[A-Z])(?=.[a-z])|(?=.\d)(?=.[A-Z])(?=.[^A-Za-z0-9\s]))(?=(.)(?>.?(?!\1})(.))(?>.?(?!\1}|\2)(.))(?>.?(?!\1|\2|\3)(.))(?>.?(?!\1|\2|\3|\4)(.))(?>.?(?!\1|\2|\3|\4|\5).))(?!.?\d{4})(?!.?[a-z]{4})(?!.?[A-Z]{4})(?!.*?[^A-Za-z0-9\s]{4})

Here’s hoping we never have to touch it again πŸ˜‰ With more time if this crops up again I’ll push the code option I think πŸ™‚

Edit: Discovered that the string isn’t quite right. It’s not passing “!tt23yyy” without having to add another digit or special character. So have canned the regex idea and am going with the code option. It’s just too hard to debug regex issues if you don’t comprehend regex πŸ™‚ (understandably so)

  • 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-23T15:37:07+00:00Added an answer on May 23, 2026 at 3:37 pm

    Here is a PCRE/Perl regex that would do all that:

    /
    ^ # anchor it
    
    # must be at least 8 characters
    (?=.{8,})
    
    # must have characters from at least 3 of the 4 different character types
    (?: 
      (?=.*\d)(?=.*[A-Z])(?=.*[a-z])
    | (?=.*\d)(?=.*[^A-Za-z0-9\s])(?=.*[a-z])
    | (?=.*[^A-Za-z0-9\s])(?=.*[A-Z])(?=.*[a-z])
    | (?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9\s])
    )
    
    # at least 5 different chars
    (?=
                           (.)
      (?>.*?(?!\1})        (.))
      (?>.*?(?!\1}|\2)     (.)) 
      (?>.*?(?!\1|\2|\3)   (.)) 
      (?>.*?(?!\1|\2|\3|\4) . )
    )
    
    # no long sequence of the same character type (if long is 3)
    (?!.*?\d{3})
    (?!.*?[a-z]{3})
    (?!.*?[A-Z]{3})
    (?!.*?[^A-Za-z0-9\s]{3})
    
    /xs
    

    Not tested so could have missed something. Enjoy. πŸ˜‰

    If you are really going to be using that (on longer strings), you might want to add some (more) atomic grouping (?>foo) (or the like) to prevent exponential backtracking.

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

Sidebar

Related Questions

I must have a mental block, but I simply can't find the reverse of
This should be simple but I guess I'm hitting a mental block- I have
This seems like a simple problem but I have a coder's mental block: The
I'm sure this is fairly simple, however I have a major mental block on
Having a mental block with going around this query. I have the following tables:
This seems to be extremely simple, but if I've got a mental block. I
I seem to have a mental block, and cannot progress with the following problem.
I like using sentry classes in c++, but I seem to have a mental
This is a stupid question, it feels like one. But mental block is bad
I know this should be painfully simple, but I'm having a mental block. 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.