Regular expressions are used for defining String patterns that can be used for searching, manipulating and editing a text. These expressions are also known as Regex (short form of Regular expressions).

Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called "metacharacters". If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash.

Character Classes or Character Sets

A "character class" matches only one out of several characters. To match an a or an e, use [ae]. You can use a hyphen inside a character class to specify a range of characters. Typing a caret after the opening square bracket negates the character class.

Shorthand Character Classes

\d matches a single character that is a digit, \w matches a "word character" (alphanumeric characters plus underscore), and \s matches a whitespace character (includes tabs and line breaks).

Non-Printable Characters

You can use special character sequences to put non-printable characters in your regular expression. Use \t to match a tab character (ASCII 0x09), \r for carriage return (0x0D) and \n for line feed (0x0A).

The Dot Matches (Almost) Any Character

The dot matches a single character, except line break characters.

Anchors

Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string.

Alternation (|)

Alternation is the regular expression equivalent of "or".

Repetition

The question mark makes the preceding token in the regular expression optional.

Grouping and Capturing

Place parentheses around multiple tokens to group them together.

Quantifiers

* matches any number of what's before it, from zero to infinity.
? matches zero or one.
+ matches one or more.