Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Regular expressions in python
  5. Patterns

Patterns

Regular expression in is a sequence of characters that forms a search pattern.

It is used to confirm whether the string has that particular term or not.

We can easily import regular expression in python.

  • [] – A set of characters
  • \ – Signals a special sequence.
  • . – Any character
  • ^ – Starts with
  • $ – Ends with
  • * – Zero or more occurrence
  • + – One or more occurrence
  • {} – Exactly the specified number of occurrences.
  • | – Either or
  • () – Capture and group
  • \d Matches any decimal digit, this is equivalent to the set class [0-9].
  • \D Matches any non-digit character.
  • \s Matches any whitespace character.
  • \S Matches any non-whitespace character
  • \w Matches any alphanumeric character, this is equivalent to the class [a-zA-Z0-9_].
  • \W Matches any non-alphanumeric character.
  • (?:re) – Groups regular expression without remembering matched text.
  • (?imx: re) – Temporarily toggles on i,m or x options within parentheses.
  • (?#…) – comment
  • (?! re) – Specifies position using pattern negation. Doesn’t have a range.
  • (? > re) Matches independent pattern without backtracking.

This is all about patterns in regular expression in python.

Was this article helpful to you? Yes No

How can we help?