I'm using Python 2.7.
I would like to know the difference between *
and .*
while matching the words.
Following is the code in python
exp = r'.*c' #here is the expression
line = '''abc dfdfdc dfdfeoriec''' #the words I need to match
re.findall(exp,line) #python expression
The output from the above code is:
['abc dfdfdc dfdfeoriec']
If I change exp
value to:
exp = r'*c'
...then on execution I get the following error:
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
Here is another code
exp = r'c.*'
line1='''cdlfjd ceee cll'''
re.findall(exp,line1)
The output from above code is
['cdlfjd ceee cll']
If I change the exp
value to:
exp = r'c*'
and then on execution I get the following output.
['c', '', '', '', '', '', '', 'c', '', '', '', '', 'c', '', '', '']
Please explain this behavior.