python - Check to ensure a string does not contain multiple values -
**note- not testing @ end of string-- need locate particular substrings anywhere in string
what fastest way check make sure string not contain multiple values. current method inefficient , unpythonic:
if string.find('png') ==-1 , sring.find('jpg') ==-1 , string.find('gif') == -1 , string.find('youtube') == -1:
if you're testing end of string, remember str.endswith can accept tuple.
>>> "test.png".endswith(('jpg', 'png', 'gif')) true
otherwise:
>>> import re >>> re.compile('jpg|png|gif').search('testpng.txt') <_sre.sre_match object @ 0xb74a46e8> >>> re.compile('jpg|png|gif').search('testpg.txt')
Comments
Post a Comment