1370 Contributors: 31 Saturday, July 29, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial:
Roadmap: roadmap

Regular Expressions (Regex)


Python makes regular expressions available through the re module.

Regular expressions are combinations of characters that are interpreted as rules for matching substrings. For instance, the expression 'amount\D+\d+' will match any string composed by the word amount plus an integral number, separated by one or more non-digits, such as:amount=100, amount is 3, amount is equal to: 33, etc.


  • Direct Regular Expressions

  • re.match(pattern, string, flag=0) # Out: match pattern at the beginning of string or None

  •, string, flag=0) # Out: match pattern inside string or None

  • re.findall(pattern, string, flag=0) # Out: list of all matches of pattern in string or []

  • re.finditer(pattern, string, flag=0) # Out: same as re.findall, but returns iterator object

  • re.sub(pattern, replacement, string, flag=0) # Out: string with replacement (string or function) in place of pattern

  • Precompiled Regular Expressions

  • precompiled_pattern = re.compile(pattern, flag=0)

  • precompiled_pattern.match(string) # Out: match at the beginning of string or None

  • # Out: match anywhere in string or None

  • precompiled_pattern.findall(string) # Out: list of all matching substrings

  • precompiled_pattern.sub(string/pattern/function, string) # Out: replaced string

Related Examples