A regular expression is a pattern used in a scripting languages such as the Sed, Awk, PHP, Perl, Bash and other programming languages to match subject string. It is used to search a string (or text) to match all occurances of a pattern, and allows it to act on it (i.e. replace with another string).
A regular expression uses a pattern to match a string. There are literal characters which matches exact character, and wildcard characters that matches a variety of characters. Also, there are symbols that match number of occurances (?, + and *). To give you a flavor of how regular expression works, here are some concepts used to match a text.
Meta Character | Operation | Example | Description |
---|---|---|---|
| | Or | apple|orange|pear | Matches apple, orange, or pear |
() | Group | gr(a|e)y | Matches grey or gray |
. | Any char | a.c | Matches abc, adc, aMc |
? | 0 or 1 occurance | colou?r | Matches color or colour |
* | 0 or more occurance | xyz* | Matches xy, xyz, xyzz |
+ | 1 or more occurance | xyz+ | Matches xyz, xyzz, xyzzz |
- | Range with bracket | [a-z] | Matches a, b, through z |
[] | Or 1 character | [abc] | Matches a, b or c |
[^ ] | Not contained | [abc] | Matches any char but a, b or c |
^ | Beginning of string | ^abc | Matches starting with abc |
$ | End of string | xyz$ | Matches string ending xyz |
\t | Tab | \t | Matches tab |
\n | New Line | \n | Matches new line |
\n, n=1..9 | Match nth item | (abc|xyz)\1 | Matches abcabc, abcxyz |
{n}, n=1..9 | match n times | a{3} | Matches aaa |