Introduction and Basic Functions

2 min read ·

Regular Expressions are used to search, match, and manipulate text based on patterns. Python provides a built in module called re to work with regular expressions.

re Module

To use regular expressions in Python, you need to import the re module.

match()

The match() function checks for a match only at the beginning of the string.

If match is found:

Note

match() only works if the pattern is at the start of the string.


search()

The search() function scans the entire string and returns first match.


findall()

The findall() function returns all matches in a list.

finditer()

The finditer() function returns an iterator of match objects.

Pro Tip

Use findall() when you want list of results and finditer() when you need position and detailed info.


Understanding Difference

  • match() → checks from beginning
  • search() → finds first occurrence anywhere
  • findall() → returns all matches as list
  • finditer() → returns match objects with details

Caution

Regular expressions are case sensitive by default.


Exercise

  • Use match() to check if string starts with a word
  • Use search() to find word inside string
  • Use findall() to get all occurrences
  • Use finditer() to print match positions