The optional second parameter pos gives an index in the string where the search is to start; it defaults to 0. This is not completely equivalent to slicing the string; the '^' pattern character matches at the real beginning of the string and at positions just after a newline, but not necessarily at the index where the search is to start.
示例:
import re
s = 'this is a test 4242 did you get it'
pat = re.compile('[a-zA-Z]+ ([0-9]+)')
print pat.match(s, 10).group(0)
^{} 不直接支持这一点。但是,如果使用^{} 预编译正则表达式(通常是个好主意),那么^{} 的类似方法^{} (和^{} )都采用可选的pos参数:
示例:
输出:
尽管} 模块(旨在取代} 模块可以通过pip安装,并适用于Python版本2.5到3.4。举个例子:
re.match
不支持这一点,但是新的^{re
模块)拥有大量新特性,包括pos
和endpos
参数,用于search
、match
、sub
和subn
。尽管还不是官方的,^{相关问题 更多 >
编程相关推荐