Regular Expressions Regex
suggest changeIntroduction
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 of Direct Regular Expressions
re.match(pattern, string, flag=0)
# Out: match pattern at the beginning of string or Nonere.search(pattern, string, flag=0)
# Out: match pattern inside string or Nonere.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 objectre.sub(pattern, replacement, string, flag=0)
# Out: string with replacement (string or function) in place of pattern
Syntax of Precompiled Regular Expressions
precompiled_pattern = re.compile(pattern, flag=0)
precompiled_pattern.match(string)
# Out: match at the beginning of string or Noneprecompiled_pattern.search(string)
# Out: match anywhere in string or Noneprecompiled_pattern.findall(string)
# Out: list of all matching substringsprecompiled_pattern.sub(string/pattern/function, string)
# Out: replaced string
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents