[Tutor] Need help on regular expresions
Richard Damon
Richard at Damon-Family.org
Sat Nov 6 11:29:04 EDT 2021
My first thought is that this sounds very much like a case of having a
hammer, so you want to treat something as a nail even if it isn't really
one.
If this is a homework problem about thinking about regular expressions,
that would be one thing, but if the problem really is identifying if the
string has the right character membership, regular expressions are not
the right tool.
Simplest would seem to be to iterate through the string, classify each
character and keep a count, and then check those counts.
A regular expression can test a string for the presence of at least 3
digits with a pattern something like (in english):
0 or more characters, 1 digit, 0 or more characters, 1 digit, 0 or more
characters, 1 digit, 0 or more characters.
Using patterns like this you can check for each category requirement
with a separate expression.
Getting a regular expression to check multiple parallel checks at the
same times gets into much more advanced topics, and I would need to
double check that python supports that level of syntax, but even if it
does, unless there is some real reason it needs to be a single regular
expression, it is much clearer to use multiple expressions for each
requirement.
On 11/6/21 11:01 AM, Manprit Singh wrote:
> Dear sir ,
>
> A little modification in my last mail,
>
> Dear Sir ,
> 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)
>
> How would I do it with regular expressions ? Since this problem will be
> very ugly with regular string methods, that's why I would like to try it
> with re's.
>
> The below given example only checks if the string x is made up of 10
> characters and contains numbers from 0-9 an underscore and alphabets a-z
> and A-Z, but no count checks of numbers and alphabets and underscore . How
> to do it?
>
> import re
> x = "aG4cD1_23fg"
> if re.fullmatch(r'\w{10}', x):
> print("Match found")
> else:
> print("No match")
>
> Kindly guide
>
> Regards
> manprit Sing
>
> On Sat, Nov 6, 2021 at 8:23 PM Manprit Singh <manpritsinghece at gmail.com>
> wrote:
>
>> Dear Sir ,
>> Let's say i have to find if a string in question is of length 10, contains
>> at least four digits, one underscore and all other 5 characters must be
>> alphabets (upper or lowercase)
>>
>> How would I do it with regular expressions ? Since this problem will be
>> very ugly with regular string methods, that's why I would like to try it
>> with re's.
>>
>> The below given example only checks if the string x is made up of 10
>> characters and contains numbers from 0-9 an underscore and alphabets a-z
>> and A-Z, but no count checks of numbers and alphabets and underscore . How
>> to do it?
>>
>> import re
>> x = "aG4cD1_23fg"
>> if re.fullmatch(r'\w{10}', x):
>> print("Match found")
>> else:
>> print("No match")
>>
>> Kindly guide
>>
>> Regards
>> manprit Singh
>>
>>
>>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
--
Richard Damon
More information about the Tutor
mailing list