The .match() method returns a matching character pattern at the beginning of a given string.


re.match(<pattern>, string, <flags>)

A <pattern> can include any of the following:

  • A string: Jane
  • A character class code: /w, /s , /d
  • A regex symbol: $, |, ^

The <flags> are optional and can be set to IGNORECASE, VERBOSE, or DOTALL.

Note: .search() will only return the the first match (as a match object) within the string; alternatively, the .findall() method matches every occurrence (and returns a list of matches).


In the example below, the .match() method is used to find a pattern at the beginning of the string:

import re
result = re.match(r"www", "")

The output will look like this:

<_sre.SRE_Match object; span=(0, 3), match="www">

Codebyte Example

The following example returns None because the test pattern is not at the beginning of the string:

import re
result = re.match(r"codecademy", "")


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy