Home > Software engineering >  Testing whether a large list of alpha characters is contained in a string (without using RegEx)
Testing whether a large list of alpha characters is contained in a string (without using RegEx)

Time:02-10

Without using RegEx, I'd like to be able to test whether a given string contains certain lowercase characters (e.g. 22 of the 26 lower-case characters in the alphabet).

For example:

my_str = 'foobar'
if 'c' in str or 'd' in my_str or 'e' in my_str:   # I don't want to enumerate the entire alphabet
    print('fails the test')
else:
    print('passes the test')

This approach works, but I would have to enumerate most of the lowercase alphabet. This is a hacky approach!

Is there another way to do this? (yes, I know that RegEx would be a good choice, but not for my use case)

Thanks!

CodePudding user response:

Just make a set and perform a set &:

>>> set('foobar') & set('cde')
set()

An empty set is equivalent to all your or's

And it will work with 22 characters.

So your if could be written as:

t = ('foobar','coobar')
for s in t:
    if set(s) & set('cde'):
        print(f'"{s}" fails the test')
    else:
        print(f'"{s}" passes the test')

Prints:

"foobar" passes the test
"coobar" fails the test

CodePudding user response:

You could use a set with the isdisjoint method:

my_str = 'foobar'
if not set('cde').isdisjoint(my_str): 
    print('fails the test')
else:
    print('passes the test')

If you want to exclude most letters, you can setup your set with all letters and remove the ones that are acceptable:

import string

excludeSet = set(string.ascii_lowercase)
excludeSet -= set('aeiou')               # e.g. allow vowels

my_str = 'foobar'
if not excludeSet.isdisjoint(my_str): 
    print('fails the test')
else:
    print('passes the test') 
  •  Tags:  
  • Related