Regex No Character Should Repeat

no characters should repeat condition: regex to match a word with unique (non-repeating) characters

match exact number of characters condition: Regular expression to match exact number of characters?

the must contain condition: Regex must contain specific letters in any order

should contain only condition: it seems from the question you have already figured that out on your own.

The remaining job is to combine them all which you should do on your own if its part of an exercise given to you.


You may use this regex with 3 lookahead assertions, satisfying all your conditions:

^(?=(?:[a-z\d]*[A-Z]){2})(?=(?:\D*\d){3})(?:([a-zA-Z\d])(?!.*\1)){10}$

RegEx Demo

RegEx Description:

  • ^: Start
  • (?=(?:[a-z\d]*[A-Z]){2}): Lookahead to assert that we have at least 2 uppercase alphabets
  • (?=(?:\D*\d){3}): Lookahead to assert that we have at least 3 digits
  • (?:([a-zA-Z\d])(?!.*\1)){10}: Match exact 10 alphanumeric characters. Negative lookahead is to assert that we don't have anything repeating anywhere.
  • $: End

Read more about look aheads and look behinds

Code:

reg = re.compile(r'^(?=(?:[a-z\d]*[A-Z]){2})(?=(?:.*\d){3})(?:([a-zA-Z\d])(?!.*\1)){10}$')
arr = ['A1b2c3d4eF', 'B1CDEF2354', 'aBcDdef122', 'B1CD102354', 'a1bcf2coqb', 'a1bcf2oo3b', '1234567890']

for i in arr:
      print i, reg.match(i)

import re
n = int(input())
patt = r'^(?=(?:[a-z\d]*[A-Z]){2})(?=(?:\D*\d){3})(?:([a-zA-Z\d])(?!.*\1)){10}$'
for x in range(n):
    match=re.match(patt,str(input()))
    if match:
        print("Valid")
    else:
        print("Invalid")