One Pager Cheat Sheet
Search engines and text editors use **regular expressions** to quickly locate data and filter out the relevant information.
- Regular expressions, or regex, are a special sequence of characters that define a pattern to
match
text, which can be more powerful and shorter than the ‘equal’ operator (==
),indexing
, or otherstring methods
. - The
import
keyword is used toload
there
module
in Python, and thereforeimport re
is a valid statement. - The
re
module provides a basic search method,.search(regex, string)
, which scans through a string and looks for a match to the provided regular expression, as demonstrated in a simple example. - Using special characters (or
metacharacters
) in regular expressions adds unique meaning to the expression, typically demonstrated by defining a character class with square brackets ([ ]
). - Using
Square Brackets
,[a-z]
,[0-9]
, and.
we can createRegex
patterns to match character ranges, digits, and consecutive characters to achieve desired results. - The
^
metacharacter allows us to match multiple strings that start with the same characters. - The metacharacter
+
checks if the previous character appears one or more times. - By using the
*
metacharacter, one can avoid having to specify the preceding character when searching for a repeating character in a string. - The basics of regular expressions were just covered, but with
metacharacters
such asSquare Brackets
,Period
,Caret
,Dollar
,Asterisk
,Plus
,\w
,\W
,\s
,\S
,\d
,\D
,\t
and\n
, one canmatch complex patterns
in text.