Difference between * and .* in regular expressions (using python)
Question: 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.


Answer: From the [docs](http://docs.python.org/2/library/re.html#regular-expression-
syntax):

    
    
    '*'
    

> Causes the resulting RE to match 0 or more repetitions of the preceding RE,
> as many repetitions as are possible. ab* will match ‘a’, ‘ab’, or ‘a’
> followed by any number of ‘b’s.

IN `r'*c"` you've no preceding character to repeat, so it's an error.

    
    
    >>> import re
    >>> strs = "ccceeeddc"
    >>> re.findall(r'c*',strs)
    ['ccc', '', '', '', '', '', 'c', '']
             |   |   |   |   |        |
             e   e   e   d   d        nothing found after last `c`
    

`c*` means find all `'c'`s(0 to any number of time) that are next to each
other and group them , so here when it reaches `'e'` no `'c'` is found so it
returns en empty string.

`'.*c'`: Group everything up to the last `c` found.

    
    
    >>> strs = "abccccfoocbar"
    >>> exp = r'.*c'
    >>> re.findall(exp,strs)
    ['abccccfooc']
    
    >>> strs = "qwertyu"
    >>> re.findall(exp,strs)  #no 'c' found
    []
    

`'c.*'`: This is exact opposite of the last one, group all characters that are
fuund after first 'c'.

    
    
    >>> exp = r'c.*'
    >>> strs = "abccccfoocbar"
    >>> re.findall(exp,strs)
    ['ccccfoocbar']
    
    >>> strs = "qwertyu"
    >>> re.findall(exp,strs) #no 'c' found
    []
    

