Basic Patterns and Special Characters:
1. Literal Characters: Characters in a regular expression match themselves. For example, the regular expression /cat/
matches the string "cat."
2. Character Classes: Square brackets []
are used to define character classes. [abc]
matches either 'a,' 'b,' or 'c.'
3. Quantifiers: Quantifiers specify the number of times a character or group should be matched:
*
: Matches zero or more occurrences.
+
: Matches one or more occurrences.
?
: Matches zero or one occurrence.
{n}
: Matches exactly
n
occurrences.
{n,}
: Matches
n
or more occurrences.
{n,m}
: Matches between
n
and
m
occurrences.
4. Metacharacters: Some characters have special meanings in regular expressions and need to be escaped with a backslash (\
) to match them literally. For example, \.
matches a period, and \\
matches a backslash.
5. Anchors: Anchors are used to match patterns at specific positions:
^
: Matches the start of a string.
$
: Matches the end of a string.
Character Classes and Shorthand:
\d
: Matches any digit character (equivalent to [0-9]
).
\D
: Matches any non-digit character (equivalent to [^0-9]
).
\w
: Matches any word character (equivalent to [a-zA-Z0-9_]
).
\W
: Matches any non-word character (equivalent to [^a-zA-Z0-9_]
).
\s
: Matches any whitespace character (spaces, tabs, line breaks).
\S
: Matches any non-whitespace character.
Groups and Alternation:
Parentheses ()
are used to group characters and create sub-patterns. The vertical bar |
represents alternation (logical OR).