title: Python regular expressions (extracting monetary value from a string)
body: <p>I am munging data which contains strings of the form:</p>
  
  <pre><code>" The little £250,000 brown fox jumped over the lazy dog" 
  </code></pre>
  
  <p><strong>[Clarification]</strong></p>
  
  <p>The string to be converted to a double may have been "cleaned" already and may simply look like this:</p>
  
  <pre><code>"£250,000"
  </code></pre>
  
  <p>I managed to hack together a crude function, that bludgeons a string like "£250,000" into a double. However, I am at my wits end to write a regex to grok the monetary part of the string, when presented with a string like the one above. The problem is that I don't know which type of string I will encounter - i.e. either a 'cleaned' monetary value (like the 2nd example) or a 'dirty' string (like the first example). I need to write a single function that handles both types of strings and returns the monetary string as a double.</p>
  
  <p>For 'dirty' strings, can someone recommend how to pattern match the monetary values (expected minimum: £1, expected maximum £99,999,999). Also, if there is a more pythonic way of writing the function below, I'd like to hear recommendations from the Pythonistas out there.</p>
  
  <pre><code>    non_numeric = re.compile(r'[^0-9\.]+')
  
      def string_to_decimal(s):
          try:
              s= s.decode('ascii')
          except:
              s = s[1:] # Assumption is that s begings with currency symbol
  
          s = str(s)  # Probably superfulous?
  
          s = s.replace(',','')
          s = non_numeric.sub('', str(s))
          return decimal.Decimal(s)
  </code></pre>