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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -