Is there any way to make sure that only the characters 'm' 'c' 'b' are in a string without resorting to regex?
For instance, if the user inputs 'm', the program will print 'Major'. If the user inputs 'mc', the program will print 'Major, Critical'.
So I want to make sure that if the user inputs something like 'mca', the program will print 'Not applicable'.
try:
if 'a' in args.findbugs:
if len(args.findbugs) > 1:
print 'findbugs: Not an applicable argument.'
else:
print 'FINDBUGS:ALL'
else:
if 'm' in args.findbugs:
print 'FINDBUGS:MAJOR'
if 'c' in args.findbugs:
print 'FINDBUGS:CRITICAL'
if 'b' in args.findbugs:
print 'FINDBUGS:BLOCKER'
except TypeError:
print "FINDBUGS: NONE"
Well, the simplest way from what you've described would be:
some_string = 'mca'
if set(some_string) <= {'m', 'c', 'b'}:
# The string contains only 'm', 'c', or 'b'.
else:
# The string 'mca' does not match because of 'a'.
Or, if you intend to require at least m
, c
, or b
:
some_string = 'mca'
if set(some_string) & {'m', 'c', 'b'}:
# The string contains 'm', 'c', or 'b', so 'mca' will match.
NOTE: As pointed out by bgporter, the set literal notation is not available in Python versions less than 2.7. If support for those is required, use set(('m', 'c', 'b'))
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments