any(iterable)函数:Return True if bool(x) is True for any x in the iterable.If the iterable is empty,return False.
str.startswith(str[,beg,end])函数:参数中的str是检测是否出现的字符串,beg是检测开始的位置(默认为0),end是检测结束的位置。参数str可以替换为tuple,使用效果是匹配tuple中任一元素,匹配上即可。这个函数匹配整体字符串str是否以参数str开始,开始返回True,否则返回False。
str.endswith(str[,beg,end])函数:与str.startswith()类似
str.find(sub[,beg,end])函数:Return the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.
If you’re going to perform a lot of matches using the same pattern, it usually pays to precompile(预编译) the regular expression pattern into a pattern object first.
datepat=re.compile(r’\d+’)
datepat.match()
re.match() always tries to find the match at the start of a string. If you want to search text for all occurrences of a pattern, use the findall() method instead. For example:
>>> text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
>>> datepat.findall(text)
['11/27/2012', '3/13/2013']
>>>
For more complicated patterns, use the sub() functions/methods in the re module. To illustrate, suppose you want to rewrite dates of the form “11/27/2012” as “2012-11-27.” Here is a sample of how to do it:
>>> text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
>>> import re
>>> re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text)
'Today is 2012-11-27. PyCon starts 2013-3-13.'
>>>
The first argument to sub() is the pattern to match and the second argument is the replacement pattern. Backslashed digits such as \3 refer to capture group numbers in the pattern.