Regular expression or shortened as regex is a sequence of characters to define a search pattern.
Regex allows us to search for specific, standard textual syntax for representing patterns for matching text.
Though look complicated, regex are very powerful as it can be used to create and match any text pattern.
Usually, operations such as string searching algorithms and input validation solved by using regex.
Thus, regex is used in search engines and in search and replace dialog of text editors.
Table of contents
- Metacharacter
- [^ (Caret)](#caret)
- . (Dot)
- [ ]
- [^ ]
- $ (Dollar)
- * (Asterisk)
- + (Plus)
- ? (Question mark)
- {n}
- {n,m}
- | (Pipe)
- Example usage
- References
Metacharacter
Metacharacter is a character that has a special meaning to a regex.
Following are the common metacharacters in regex and description:
^ (Caret)
Matches the starting position within the string.
. (Dot)
Matches any single character. Can be used as wildcard character.
[ ]
Matches a single character that is contained within the bracket.
-
can be used to specific a range of characters. Thus, [a-z]
matches "a" until "z".
[^ ]
Matches a single character that is not contained within the bracket.
-
can be used to specific a range of characters. Thus, [^a-z]
matches any except "a" until "z".
$ (Dollar)
Matches the ending position of the string.
* (Asterisk)
A repeater where matches when the character preceding *
matches 0 or more times.
+ (Plus)
A repeater where matches when the character preceding +
matches at least one or more times.
? (Question mark)
Matches when the character preceding ?
occurs 0 or 1 time only, making the character optional.
{n}
Matches when the preceding character occurs n times.
{m,n}
Matches when the preceding character occurs at least m and not more than n times.
| (Pipe)
Matches either the expression before or expression after the |
.
Example usage
Following are common usages of regex.
If the usage required to use metacharacter as literal character, we can used backslash \
to escape the character.
Email validation
^[a-z0-9_\.]+@[a-z]+\.[a-z]{2,3}$
Phone validation
Regex is used to match phone validation used in Malaysia.
^\+6[0-9]{2,3}-[0-9]{7,8}$