Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex No Character Should Repeat

I have just started out on the regex part of python and I thought I understood this concept but when I started out the programming I wasn't able to get it. The problem statement which was given is to design a regex which

  1. It must contain at least 2 uppercase English alphabet characters
  2. It must contain at least 3 digits (0-9)
  3. it should only contain alphanumeric characters
  4. No characters should repeat
  5. There must be exactly 10 characters

The code which I wrote is

import re
n=int(input())
patt=r'^(?=.*[A-Z]).{2,}(?=.*[0-9]).{3,}(?=.*[\w]?){10}$'
for x in range(n):
    match=re.findall(patt,str(input()))
    #print(match)
    if match:
        print("Valid")
    else:
        print("Invalid")

I first started out with the 1st part i.e should contain "It must contain at least 2 uppercase English alphabet characters" for which I wrote (?=.*[A-Z]).{2,} as it will search for more than two characters and will use lookahead assertions For the second part I applied the same and for the third part i.e it should only contain alphanumeric characters I applied (?=.*[\w]?) these three seems to work but when the fourth and fifth condition comes i.e No characters should repeat and There must be exactly 10 characters I tried to use {10} at last but it didn't work and the whole thing seems to be broken now. Can anyone guide me how to use regex and what exactly is a positive lookahead.

like image 469
Anidh Singh Avatar asked Jul 16 '18 09:07

Anidh Singh


People also ask

What does the plus character [+] do in regex?

The plus ( + ) is a quantifier that matches one or more occurrences of the preceding element. The plus is similar to the asterisk ( * ) in that many occurrences are acceptable, but unlike the asterisk in that at least one occurrence is required.

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What does 1 mean in regex?

Formatting regular expressions The default setting is \1, which means that a zone name that matches the regular expression is replaced by the contents of the first variable created by the regular expression. In a regular expression, variable values are created by parenthetical statements.


1 Answers

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.

like image 116
Ishan Srivastava Avatar answered Oct 20 '22 23:10

Ishan Srivastava