Python TutorialLesson 30Free preview
Python RegEx
This lesson is part of Python · All levels
Summary
Regular expression or regex is a pattern of text for searching, matching, and replacing a string based on a specific rule. The re built-in module provides functions such as search(), findall(), and sub() to implement the pattern. Specific characters like \d for digits or \s for spaces allow for highly flexible matching.
Key points
- The re module is used to work with regular expression.
- Re.search() searches for the first appearance, while re.findall() collects all matches.
- Re.sub() replaces the text that matches the pattern.
- Write the pattern as a raw string r"..." to avoid misinterpretation of the backslash.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.