trying to regex in python
Question: _Can anyone please help me understand this code snippet,
from<http://garethrees.org/2007/05/07/python-challenge/> Level2_

    
    
    >>> import urllib
    >>> def get_challenge(s):
    ...     return urllib.urlopen('http://www.pythonchallenge.com/pc/' + s).read()
    ...
    >>> src = get_challenge('def/ocr.html')
    >>> import re
    >>> text = re.compile('<!--((?:[^-]+|-[^-]|--[^>])*)-->', re.S).findall(src)[-1]
    >>> counts = {}
    >>> for c in text: counts[c] = counts.get(c, 0) + 1
    >>> counts
    

<http://garethrees.org/2007/05/07/python-challenge/>

`re.compile('<!--((?:[^-]+|-[^-]|--[^>])*)-->', re.S).findall(src)[-1]` why we
have [-1] here what's the purpose of it? is it Converting that to a list? **


Answer: Yes. `re.findall()` returns a list of all the matches. Have a look at [the
documentation](https://docs.python.org/3.4/library/re.html).

> _re.findall(pattern, string, flags=0)_
>
> Return all non-overlapping matches of pattern in string, **as a list of
> strings.** The string is scanned left-to-right, and matches are returned in
> the order found. If one or more groups are present in the pattern, return a
> list of groups; this will be a list of tuples if the pattern has more than
> one group. Empty matches are included in the result unless they touch the
> beginning of another match.

When calling `[-1]` on the result, the first element _from the end of the
list_ is accessed.

For example;

    
    
    >>> a = [1,2,3,4,5]
    >>> a[-1]
    5
    

And also:

    
    
    >>> re.compile('.*?-').findall('-foo-bar-')[-1]
    'bar-'
    

