Regular Expressions › Key Functions and Parameters

Michael BrenndoerferUpdated March 18, 20252 min read
Language AI HandbookAll chapters
Part 1 of 67Text as Data
Chapter 3Regular Expressions › Key Functions and Parameters
Reading section 19 of 21 · Key Functions and Parameters · 2 minSection 19 of 21 · 2 min

Book contents

Section 19 of 21 · 2 min read

Regular Expressions › Key Functions and ParametersOne section · Sections in this chapter19 / 21 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

When working with regular expressions in Python, these are the essential functions and their most important parameters:

re.search(pattern, string, flags=0)

  • pattern: The regex pattern to search for
  • string: The text to search within
  • flags: Optional modifiers like re.IGNORECASE, re.MULTILINE
  • Returns: A match object for the first match, or None if no match

re.findall(pattern, string, flags=0)

  • Returns all non-overlapping matches as a list of strings
  • If the pattern has groups, returns a list of tuples containing the groups

re.finditer(pattern, string, flags=0)

  • Returns an iterator of match objects for all matches
  • Use when you need position information or access to groups

re.sub(pattern, repl, string, count=0, flags=0)

  • repl: Replacement string or function
  • count: Maximum number of replacements (0 means all)
  • Backreferences like \1 can be used in the replacement string

re.split(pattern, string, maxsplit=0, flags=0)

  • maxsplit: Maximum number of splits (0 means no limit)
  • Returns a list of strings split at pattern matches

re.compile(pattern, flags=0)

  • Pre-compiles a pattern for repeated use
  • Returns a compiled pattern object with the same methods

Common Flags

  • re.IGNORECASE (or re.I): Case-insensitive matching
  • re.MULTILINE (or re.M): ^ and $ match at line boundaries
  • re.DOTALL (or re.S): . matches newlines
  • re.VERBOSE (or re.X): Allow comments and whitespace in patterns