Regular expressions (regex) are powerful tools used for pattern matching and text processing. Python provides the re
module to work with regular expressions.
Regular expressions consist of special characters and sequences that define search patterns. Here are some common elements:
.
- Matches any character except a newline\d
- Matches any digit (equivalent to [0-9])\w
- Matches any alphanumeric character (equivalent to [a-zA-Z0-9_])\s
- Matches any whitespace character (space, tab, newline)^
- Matches the start of a string$
- Matches the end of a string*
- Matches zero or more occurrences of the preceding element+
- Matches one or more occurrences of the preceding element?
- Matches zero or one occurrence of the preceding element[]
- Matches any single character within the brackets|
- Acts as a logical OR between patterns()
- Groups patterns together\
- Escapes special characters{n}
- Matches exactly n occurrences of the preceding element{n,}
- Matches n or more occurrences of the preceding element{n,m}
- Matches between n and m occurrences of the preceding element\b
- Matches a word boundary\B
- Matches a non-word boundary(?P<name>pattern)
- Named group(?P=name)
- Matches the text matched by a named group(?=pattern)
- Positive lookahead assertion(?<!pattern)
- Negative lookahead assertion(?<=pattern)
- Positive lookbehind assertion(?<!pattern)
- Negative lookbehind assertion(?i)
- Case-insensitive matching(?m)
- Multi-line matching(?s)
- Dot matches all (including newlines)(?x)
- Verbose mode (ignore whitespace and comments)(?P<name>pattern)
- Named capturing groupRegex 30 Examples are provided in this file : Regex 30 Examples
For Official Python Regular Expressions Documentation click official reference