Getting Started with Python re — Regular Expression Operations
re of Fortune
In analogous terms, using re in Python is similar to buying a vowel on the Wheel of Fortune…it makes solving the puzzle much simpler!
What is re?
Although re is the actual module name in Python this is just short for RegEx, or Regular Expression. Simply put, regular expressions are a sequence of characters that forms a search pattern which can be used to check over text. Regular expressions can be an extremely powerful tool as they can assist in filtering data especially in larger datasets and they also significantly reduce the amount of code needed. For example:
Why should we re?
re essentially is a programming language of its own and therefore it is quite extensive. This allows for a great deal of flexibility for your searches but also requires research and practice. Below are links to a book on regular expressions, an online regular expression tester, a YouTube tutorial on the re module, and the documentation of re in Python.
Mastering Regular Expressions
Written in the lucid, entertaining tone that makes a complex, dry topic become crystal-clear to programmers, and…
PyRegex is a online regular expression tester to check validity of regular expressions in the Python language regex…
re - Regular expression operations - Python 3.7.4rc1 documentation
The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any…
re application is heavily observed in real-life! Although the possibilities are endless, re searches for patterns within texts which can be extremely useful for industries that need to make timely decisions based on information such as finance.
To work with re in Python, we must first import the module.
A raw string in Python is simply a string prefixed with an “r”. This simply tells Python not to handle \ in any special way. For example:
This is important because we want our regular expressions to interpret the strings passed in and not have Python doing anything to them first.
The re module offers a set of functions that allows us to search a string for a match.
re.findall — Returns a list containing all matches.
re.search — Returns a match object if there is a match anywhere in the string. Only the first occurrence of the match will be returned.
re.split — Returns a list where the string has been split at each match.
re.sub — Replaces one or many matches with a string.
A special sequence is a \ followed by characters and has a special meaning like a wild card. For example, when we used the functions split and sub above, \s returns a match where the string contains a white space character.
\A – Returns a match if the specified characters are at the beginning of the string.
\Z – Returns a match if the specified characters are at the end of the string.
\d – Returns a match where the string contains digits (numbers from 0-9).
Metacharacters are characters with a special meaning
 – A set of characters
^ – Starts with
$ – Ends with
| – Either or
In this lesson on Getting Started with Python re — Regular Expression Operations, only 4 functions, 3 special sequences, and 4 metacharacters were introduced and yet we were able to observe the powerful capabilities. If you are working with large data sets regular expressions can be highly effective in returning or manipulating data with a single line of code. I hope this introduction taught you some basic fundamentals and please use PyRegex to practice your coding with re.
Create a new variable called ripped_money that is a list of the variable hundred_million where each index is a slice of where either a comma or period was. Omit the commas and period from this list. The output should look like: [‘100’, ‘000’, ‘000’, ‘00’]