Regex for matching string Python
Question: I wanted to match the numeric values of a string:

    
    
    1,000 metric tonnes per contract month
    Five cents ($0.05) per tonne
    Five cents ($0.05) per tonne
    1,000 metric tonnes per contract month
    

My current approach:

    
    
    size = re.findall(r'(\d+(,?\d*).*?)', my_string)
    

What I get with my approach:

    
    
    print size
    [(u'1,000', u',000')]
    

As you can see, the number `1` was being cut out from the second element of
the list, why is that? Also, could I get a hint as to how I can match the
`$0.05` terms?


Answer: Something like this:

    
    
    >>> import re
    >>>  strs = """1,000 metric tonnes per contract month
    Five cents ($0.05) per tonne
    Five cents ($0.05) per tonne
    1,000 metric tonnes per contract month"""
    >>> [m.group(0) for m in re.finditer(r'\$?\d+([,.]\d+)?', strs)]
    ['1,000', '$0.05', '$0.05', '1,000']
    

Demo : <http://rubular.com/r/UomzIY3SD3>

