[Tutor] Need help on regular expresions

Alan Gauld alan.gauld at yahoo.co.uk
Sat Nov 6 18:54:11 EDT 2021


> Let's say i have to find if a string in question is of length 10, contains
> four digits, one underscore and all other 5 characters must be alphabets
> (upper or lowercase)
> 

A slightly different approach:

s = "1a2b3c4dE_"

s2 = ''.join(sorted(s.lower()))
print(s2)

num,ltr = s2.split('_')
print(num,ltr)

if len(s) == 10 and len(num) == 4 and len(ltr) == 5:
	print("Success!")
	
> How would I do it with regular expressions ? Since this problem will be
> very ugly with regular string methods,

Reyes looks for patterns but you do not have a pattern, just a set 
of count based rules. Regex  is not a good solution in this case. 
Regex is great for complex  patterns but when you try to force 
it into other roles it doesn’t work so well.

Alan G.
(Sent from my iPad coz my PC is offline…)




More information about the Tutor mailing list