re
library
import re
r"sequence"
to define the regex expression
pattern = r"Cookie"
sequence = "Cookie"
if re.match(pattern, sequence):
print("Match!")
else: print("Not a match!")
search
group
re.search(r'Co.k.e', 'Cookie').group()
# Returns: 'Cookie'
.
will match any single character except the newline character^
matches the beginning of a string$
matches the end of a string[abc]
matches a
, b
, or c
[a-zA-Z0-9]
- Matches any letter from (a to z) or (A to Z) or (0 to 9)\w
- Lowercase ‘w’. Matches any single letter, digit, or underscore.\W
- Uppercase ‘W’. Matches any character not part of \w (lowercase w).\s
- Lowercase ‘s’. Matches a single whitespace character like: space, newline, tab, return.\S
- Uppercase ‘S’. Matches any character not part of \s (lowercase s).\d
- Lowercase d. Matches decimal digit 0-9.\D
- Uppercase d. Matches any character that is not a decimal digit.\t
- Lowercase t. Matches tab.\n
- Lowercase n. Matches newline.\r
- Lowercase r. Matches return.\A
- Uppercase a. Matches only at the start of the string. Works across multiple lines as well.\Z
- Uppercase z. Matches only at the end of the string.\b
- Lowercase b. Matches only the beginning or end of the word.+
- Checks if the preceding character appears one or more times starting from that position.*
- Checks if the preceding character appears zero or more times starting from that position.?
- Checks if the preceding character appears exactly zero or one time starting from that position.{x}
- Repeat exactly x number of times.{x,}
- Repeat at least x times or more.{x, y}
- Repeat at least x times but no more than y times.{ }
- Checks for an explicit number of times.( )
- Creates a group when performing matches.< >
- Creates a named group when performing matches.