Python Language Regular Expressions (Regex)

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Introduction

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.

Syntax

  • Direct Regular Expressions

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

  • re.search(pattern, 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

  • precompiled_pattern.search(string) # 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



Got any Python Language Question?