我正在使用Python 2.7。正则表达式中的*和。*之间的区别(使用python)
我想知道*
和.*
之间的区别,而匹配的话。
以下是在Python代码
exp = r'.*c' #here is the expression
line = '''abc dfdfdc dfdfeoriec''' #the words I need to match
re.findall(exp,line) #python expression
从上面的代码的输出是:
['abc dfdfdc dfdfeoriec']
如果我改变exp
值:
exp = r'*c'
...然后执行时我得到以下错误:
Traceback (most recent call last): File "<stdin>", line 1, in
<module> File "C:\Program
Files\Enthought\Canopy32\App\appdata\canopy-1.0.0.1160.win-x86\lib\re.py",
line 177, in findall
return _compile(pattern, flags).findall(string) File "C:\Program Files\Enthought\Canopy32\App\appdata\canopy-1.0.0.1160.win-x86\lib\re.py",
line 242, in _compile
raise error, v # invalid expression error: nothing to repeat
这里是另一个代码
exp = r'c.*'
line1='''cdlfjd ceee cll'''
re.findall(exp,line1)
从上面的代码的输出是
['cdlfjd ceee cll']
如果我的exp
值更改为:
exp = r'c*'
,然后执行我得到以下输出。
['c', '', '', '', '', '', '', 'c', '', '', '', '', 'c', '', '', '']
请解释此行为。
你有没有抬头的文件定期Python中的表达式? –
有一些其他的RE问题,这是一个骗局... –