#!/bin/env python
#-******************************************************************************
# Parse a string to find floats.
import re

#-******************************************************************************
# Commented regex for a full numeric parser
g_numeric_const_pattern = r"""
[-+]?  # optional sign
(?:
    (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
    |
    (?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
)
# followed by optional exponent part if desired
(?: [Ee] [+-]? \d+ ) ?
"""

#-******************************************************************************
# Compile it into a regex
g_nconst_regex = re.compile( g_numeric_const_pattern, re.VERBOSE )

#-******************************************************************************
# This returns a list of the floats that have been parsed, as strings.
def ExtractFloatStrings( s ):
    flist = g_nconst_regex.findall( s )
    return flist

#-******************************************************************************
# This returns a list of the floats that have been parsed, as floats.
def ExtractFloats( s ):
    fl = ExtractFloatStrings( s )
    ret = []
    for f in fl:
        ret.append( float( f ) )
    return ret

#-******************************************************************************
# This tests it.
if __name__ == "__main__":
    print "Extracting Float Strings"
    print ExtractFloatStrings( ".1 .12 9.1 98.1 1. 12. 1 12" )
    print ExtractFloatStrings( "-1 +1 2e9 +2E+09 -2e-9" )
    print ExtractFloatStrings( "something weird: -2.03e+99meters" )

    print "\nExtracting Floats"
    print ExtractFloats( ".1 .12 9.1 98.1 1. 12. 1 12" )
    print ExtractFloats( "-1 +1 2e9 +2E+09 -2e-9" )
    print ExtractFloats( "something weird: -2.03e+99meters" )





