Awesome regular expression (REGEX) commands anyone must know:
-
Exact Match: The most basic form of regex involves matching a sequence of characters. For instance, if you want to match 'Hello World', you can simply use
Hello World
as your regex. -
Any Character (.): The dot
.
matches any character except a newline character. For example,a.b
can match 'acb', 'a2b', 'a$b', etc. -
Zero or More (*): The asterisk
*
matches zero or more instances of the preceding character. For example,a*
can match '', 'a', 'aa', 'aaa', etc. -
One or More (+): The plus
+
matches one or more occurrences of the preceding character. For example,a+
can match 'a', 'aa', 'aaa', etc., but not an empty string. -
Zero or One (?): The question mark
?
matches zero or one occurrence of the preceding character. For example,a?
can match '' or 'a'. -
Character Set ([]): Square brackets
[]
define a character class, matching any character within the brackets. For example,[abc]
can match 'a', 'b', or 'c'. -
Ranges (-): You can specify a range of characters using
-
inside a character set. For example,[a-z]
matches any lowercase letter, and[0-9]
matches any digit. -
Negation (^): The caret
^
at the start of a character set negates the set, matching any character not in the set. For example,[^0-9]
matches any non-digit character. -
Groups and Capturing (() and $1, $2, etc.): Parentheses
()
are used to create a group, which can be referenced in the replace pattern. For example,(abc)
creates a group matching the string 'abc', and$1
in the replace pattern refers to this group. -
Alternation (|): The pipe
|
acts like a boolean OR, matching the pattern before or the pattern after it. For example,abc|def
matches 'abc' or 'def'.