原博文链接:http://www.aobosir.com/blog/2016/12/08/python-compile-Regular-Expressions-pattam/
- 使用的电脑系统:Windows 10 64位
- 使用的开发集成环境:PyCharm 2016.1.4
- 使用的Python的版本:python 2.7.10 和 python 3.5.0
compile()函数的用法
import re
str = 'fdfgdrthxxi--gdfgexxlove--dsdfwesdxxyou--dfgdf'
pattam_str = 'xx(.*?)--'
result = re.compile(pattam_str).findall(str)
print(result)
运行:
>python learningpython29.py
['i', 'love', 'you']
解释:
- 其中
pattam_str
就是正则表达式字符串。 str
就是目标字符串result
就是得到的结果
代码中的
result = re.compile(pattam_str).findall(str)
print(result)
等价于:
result = re.findall(pattam_str, str)
运行输出的结果是一样的。