Regular Expressions (Regex)

Download python eBook

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

Related Examples

Stats

1370 Contributors: 31
Saturday, July 29, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook