Python’s re module comes with a number of regular expression methods that help you achieve more with less.
Related article: Python Regex Superpower – The Ultimate Guide
Think of those methods as the framework connecting regular expressions with the Python programming language. Every programming language comes with its own way of handling regular expressions. For example, the Perl programming language has many built-in mechanisms for regular expressions—you don’t need to import a regular expression library—while the Java programming language provides regular expressions only within a library. This is also the approach of Python.
These are the most important regular expression methods of Python’s re module:
re.findall(pattern, string): Checks if the string matches the pattern and returns all occurrences of the matched pattern as a list of strings.
re.search(pattern, string): Checks if the string matches the regex pattern and returns only the first match as a match object. The match object is just that: an object that stores meta information about the match such as the matching position and the matched substring.
re.match(pattern, string): Checks if any string prefix matches the regex pattern and returns a match object.
re.fullmatch(pattern, string): Checks if the whole string matches the regex pattern and returns a match object.
re.compile(pattern): Creates a regular expression object from the pattern to speed up the matching if you want to use the regex pattern multiple times.
re.split(pattern, string): Splits the string wherever the pattern regex matches and returns a list of strings. For example, you can split a string into a list of words by using whitespace characters as separators.
re.sub(pattern, repl, string): Replaces (substitutes) the first occurrence of the regex pattern with the replacement string repl and return a new string.
Example: Let’s have a look at some examples of all the above functions:
import re text = ''' LADY CAPULET Alack the day, she's dead, she's dead, she's dead! CAPULET Ha! let me see her: out, alas! she's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. Nurse O lamentable day! ''' print(re.findall('she', text)) ''' Finds the pattern 'she' four times in the text: ['she', 'she', 'she', 'she'] ''' print(re.search('she', text)) ''' Finds the first match of 'she' in the text: <re.Match object; span=(34, 37), match='she'> The match object contains important information such as the matched position. ''' print(re.match('she', text)) ''' Tries to match any string prefix -- but nothing found: None ''' print(re.fullmatch('she', text)) ''' Fails to match the whole string with the pattern 'she': None ''' print(re.split('\n', text)) ''' Splits the whole string on the new line delimiter '\n': ['', 'LADY CAPULET', '', " Alack the day, she's dead, she's dead, she's dead!", '', 'CAPULET', '', " Ha! let me see her: out, alas! she's cold:", ' Her blood is settled, and her joints are stiff;', ' Life and these lips have long been separated:', ' Death lies on her like an untimely frost', ' Upon the sweetest flower of all the field.', '', 'Nurse', '', ' O lamentable day!', ''] ''' print(re.sub('she', 'he', text)) ''' Replaces all occurrences of 'she' with 'he': LADY CAPULET Alack the day, he's dead, he's dead, he's dead! CAPULET Ha! let me see her: out, alas! he's cold: Her blood is settled, and her joints are stiff; Life and these lips have long been separated: Death lies on her like an untimely frost Upon the sweetest flower of all the field. Nurse O lamentable day! '''
Now, you know the most important regular expression functions. You know how to apply regular expressions to strings. But you don’t know how to write your regex patterns in the first place. Let’s dive into regular expressions and fix this once and for all!
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
Where to Go From Here
You’ve learned a quick overview of the Python regular expression methods. These are the basis of all advanced regex concepts in Python.
Want to learn more about regular expressions and acquire the Python regex superpower? Read my 17,000 word tutorial on the Finxter blog!
Need to brush up your Python skills? Join the free Finxter Computer Science Email Academy!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.