|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在正则表达式扩展阅读中,有一个例子:
import re
p = re.compile(...)
a = p.match('string goes here')
if a :
print('Match found:',a.group())
else:
print('NO MATCH')
重点在(...)这个上面,在正则表达式中.表示出了空字符外的所有字符。这里用了3个.也就是表示会将‘string goes here’都找出来的。
但是当我按照上面代码实际运行时会提示一下:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
p = re.compile(...)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 233, in compile
return _compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 300, in _compile
raise TypeError("first argument must be string or compiled pattern")
TypeError: first argument must be string or compiled pattern
我更改了下变成:p = re.compile('...')加了引号,搜索结果变成了:str
问题1.(...)是否按照我开始所想是找到‘string goes here’ ? 为何报错 ?
2.使用(‘...’)的话,在这里是按照match查找前三个字符串而已? 既然能找到说明.自身含义没有消失,那应该是‘string goes here’啊,为何只有三个字符:str |
|