Metacharacters of Regular Expression

What are the metacharacters used in a Regular Expression?

Explanation

The following is the list of metacharacters used in regular expressions across different platforms.
Metacharacter Description
\ Specifies the next character as either a special character, a literal, a back reference, or an octal escape.
^ Matches the position at the beginning of the input string.
$ Matches the position at the end of the input string.
* Matches the preceding subexpression zero or more times.
+ Matches the preceding subexpression one or more times.
? Matches the preceding subexpression zero or one time.
{n} Matches exactly n times, where n is a non-negative integer.
{n,} Matches at least n times, n is a non-negative integer.
{n,m} Matches at least n and at most m times, where m and n are non-negative integers and n <= m.
. Matches any single character except "n".
[xyz] A character set. Matches any one of the enclosed characters.
x|y Matches either x or y.
[^xyz] A negative character set. Matches any character not enclosed.
[a-z] A range of characters. Matches any character in the specified range.
[^a-z] A negative range characters. Matches any character not in the specified range.
b Matches a word boundary, that is, the position between a word and a space.
B Matches a nonword boundary. 'erB' matches the 'er' in "verb" but not the 'er' in "never".
d Matches a digit character.
D Matches a non-digit character.
f Matches a form-feed character.
n Matches a newline character.
r Matches a carriage return character.
s Matches any whitespace character including space, tab, form-feed, etc.
S Matches any non-whitespace character.
t Matches a tab character.
v Matches a vertical tab character.
w Matches any word character including underscore.
W Matches any non-word character.
un Matches n, where n is a Unicode character expressed as four hexadecimal digits. For example, u00A9 matches the copyright symbol

The above metacharacter can be used with various commands to match the string like preg_match, preg_match_all in PHP or used with a "~=" to compare in Perl.

Ask Questions

Ask Question