i206: Lecture 19: Regular Expressions, cont. Marti Hearst Spring 2012
Regex for Dollars No commas With commas With or without commas \$[0-9]+(\.[0-9][0-9])? With commas \$[0-9][0-9]?[0-9]?(,[0-9][0-9][0-9])*(\.[0-9][0-9])? With or without commas \$[0-9][0-9]?[0-9]?((,[0-9][0-9][0-9])*| [0-9]*) (\.[0-9][0-9])?
Using Regex’s Two basic operations that regular expressions are used for: searching and matching. Searching: moving through a string to locate a sub-string that matches a given pattern, Matching: testing a string to see if it conforms to a pattern. After matching you might want to substitute in alternative strings, or split up the strings.
Regex in Python Python documentation on regular expressions import re result = re.search(pattern, string) result = re.findall(pattern, string) result = re.match(pattern, string) Python documentation on regular expressions http://docs.python.org/release/3.1.3/library/re.html Some useful flags like IGNORECASE, MULTILINE, DOTALL, VERBOSE A nice tutorial: http://www.macresearch.org/files/RegularExpressionsInPython.pdf
Verbose Regex’s (allows for comments and multi-line expressions) On input of: XXX,36346, 6633.334, -1