Python Regex

Created with Sketch.

Python Regex

A regular expression (or regex) is a sequence of characters that specifies a search pattern. In practice, you’ll find the regular expressions in many applications such as search engines, search and replace dialogs of text editors.

In Python, a regular expression is a separate programming language. It is embedded in Python. To interact with the regular expressions, you use the built-in re module:

Python Regex

The re module has many helpful classes and functions that help you to manage regular expressions effectively.

Section 1. Basic Regular Expressions

This section introduces the regular expression basics including functions, character sets, anchors, and word boundaries.

  • Regular expressions – learn how regular expressions work in Python and how to use functions in re module to match a string for a pattern.
  • Character sets – introduce to you the character set (\d\w\s) to match digits, word characters, and spaces.
  • Anchors – show you how to use the caret (^) and dollar ($) anchors to match at the beginning and end of a string.
  • Word Boundary – guide you on how to use word boundary (\b) to match the whole word.

Section 2. Quantifiers

This section shows you how to apply quantifiers to specific parts and two modes of quantifiers including greedy and lazy.

  • Quantifiers – learn how to use the quantifiers to match a number of instances of a character or character class.
  • Greedy Quantifiers – learn how the greedy quantifiers work.
  • Non-greedy (or lazy) Quantifiers – explain how greedy quantifiers works and show you how to turn greedy quantifiers into non-greedy quantifiers.

Section 3. Sets and Ranges

This section covers the sets and ranges to match the characters.

  • Sets & Ranges – learn how to use the sets and ranges to match a character in a set or range of characters.

Section 4. Grouping

This section shows you how to use the grouping to extract portions of information, reference the preceding group, and apply alternation.

  • Capturing groups – include a part of a match in the matches array and assign it a name using a named group.
  • Backreferences – learn about backreferences and how to apply them effectively.
  • Alternation – show you how to use the “OR” operator in regular expressions.
  • Non-capturing groups – create a group but don’t save it in the groups of the match.

Section 5. Look Around

This section explains the concepts of different types of look-around mechanisms.

  • Lookahead – introduce to you the lookahead concept and how to use it to match X only if it is followed by Y.
  • Lookbehind – explain the lookbehind concept and how to match X only if there’s is Y before it.

Section 6. Python regex functions

This section discusses the regular expression functions in the re module in detail and the regex flags.

  • findall() – find all matches that match a regular expression in a string.
  • fullmatch() – match the whole string with a pattern.
  • finditer() – return an iterator yielding Match objects over all non-overlapping matches for a regular expression in a string.
  • match() – check if zero or more characters at the beginning of a string match a regular expressesion.
  • search() – search for the first match in a string.
  • sub() – return a string with matches replaced with a replacement.
  • split() – split a string by the matches of a regular expression.
  • Regex flags – learn about the regex flags and how they change the way the regex engine matches patterns.

Section 7. Python Regex Cheat Sheet

Leave a Reply

Your email address will not be published.