Regular Expressions
Published Aug 1, 2021Updated Jul 2, 2023
Contribute to Docs
Regex, short for regular expressions, is a powerful system for searching text. Regular expressions are implemented in a variety of languages including Java and Python.
Syntax
Regular expressions are accessed by importing the re
module:
import re
regex = r"this is a regex pattern"
For the most part, regex patterns are expressed with raw string notation (hence, the preceding r
character).
The following entries explore some terms and operations related to Python regular expressions:
Regular Expressions
- Metacharacters
- Matches or qualifies regular character patterns or other expressions.
- re.findall()
- Returns a list of every pattern match that occurs in a given string.
- re.match()
- Returns a matching character pattern at the beginning of a given string.
- re.search()
- Returns the first match of a character pattern anywhere in a given string.
- re.split()
- Divides a string into substrings at each occurrence of the specified character(s).
- re.sub()
- Replace matching substrings with a new string for all occurrences, or a specified number.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours