Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Regex - Allow one or two alphabet and its can be at anywhere in string

I want regex which can fulfill below requirement:

  1. Between 6 and 10 total characters
  2. At least 1 but not more than 2 of the characters need to be alpha
  3. The alpha characters can be anywhere in the string

We have tried this but not working as expected : (^[A-Z]{1,2}[0-9]{5,8}$)|(^[A-Z]{1}[0-9]{4,8}[A-Z]{1}$)|(^[0-9]{4,8}[A-Z]{1,2}$)|([^A-Z]{3}[0-9]{6,9})

Can anyone please help me to figure it out?

Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can assert the length of the string to be 6-10 char.

Then match at least a single char [A-Z] between optional digits, and optionally match a second char [A-Z] between optional digits.

^(?=[A-Z\d]{6,10}$)\d*[A-Z](?:\d*[A-Z])?\d*$
  • ^ Start of string
  • (?=[A-Z\d]{6,10}$) Positive lookahead to assert 6-10 occurrences of A-Z or a digit
  • \d*[A-Z] Match optional digits and then match the first [A-Z]
  • (?:\d*[A-Z])? Optionally match optional digits and the second [A-Z]
  • \d* Match optional digits
  • $ End of string

See a regex demo.

about 4 years ago · Juan Pablo Isaza Report

0

One option is to use the following regular expression:

^(?=.*[a-z])(?!(?:.*[a-z]){3})[a-z\d]{6,10}$

with the case-indifferent flag i set.

Demo

This expression reads, "Match the beginning of the string, assert the string contains at least one letter, assert the string does not contain three letters and assert the string contains 6-10 characters, all being letters or numbers".

The various parts of the expression have the following functions.

^              # match the beginning of the string
(?=            # begin a positive lookahead
  .*[a-z]      # match zero or more characters and then a letter
)              # end positive lookahead
(?!            # begin a negative lookahead
  (?:          # begin a non-capture group
    .*[a-z]    # match zero or more characters and then a letter
  ){3}         # end non-capture group and execute it 3 times
)              # end negative lookahead
[a-z\d]{6,10}  # match 6-10 letters or digits
$              # match end of string

Note that neither of the lookaheads advances the string pointer maintained by the regex engine from the beginning of the string.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!