标签:python
场景:
字符串开头或者结尾匹配,一般是使用在匹配文件类型或者url
一般使用startwith或者endwith
>>> a='http://blog.csdn.net/raylee2007' >>> a.startswith ('http') True
>>> a='http://blog.csdn.net/raylee2007' >>> a.startswith (('http','ftp')) True
如果是列表或者字典,则报错
>>> a='http://blog.csdn.net/raylee2007' >>> a.startswith (['http','ftp']) Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> a.startswith (['http','ftp']) TypeError: startswith first arg must be str or a tuple of str, not list >>>
>>> a='http://blog.csdn.net/raylee2007' >>> a[0:4]=='http' True >>>
>>> import re >>> url = 'http://www.python.org' >>> re.match('http:|https:|ftp:', url) <_sre.SRE_Match object; span=(0, 5), match='http:'> >>> help(re.match ) Help on function match in module re: match(pattern, string, flags=0) Try to apply the pattern at the start of the string, returning a match object, or None if no match was found. >>>
就说到这里,谢谢大家
------------------------------------------------------------------
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:python
原文地址:http://blog.csdn.net/raylee2007/article/details/48036143