1. What is a regular expression?
The official explanation is that regular expressions are patterns that describe a set of strings. Regular expressions are constructed like arithmetic expressions and represent a set of strings by using a combination of various special symbols.
2. Meta characters
metacharacter |
describe |
Example |
. |
matches any single character |
a.: matches any character starting with a and ending with |
* |
matches the immediately preceding character any number of times (0, 1 or infinite) |
a*: matches a multiple times |
.* |
matches any character of any length |
a.*: a line of any length starting with a |
^ |
start of line locator |
^root: the line starting with root |
$ |
end of line locator |
root$: line ending with root |
[] |
matches any single character in the specified range |
[abc]: match a or b or c |
[^] |
matches any single character outside the specified range |
[^abcd]: matches any character other than a,b,c,d |
\? |
Match the preceding character 0 or 1 times |
a\?: Match a 0 times or 1 time |
\{m,n\} |
matches the preceding character at least m times and at most n times |
a\{2,5\}: match a at least 2 times and at most 5 times |
\{m,\} |
matches the preceding character at least m times |
a\{5,\}: match a at least 5 times |
\{m\} |
Match the preceding character exactly m times |
a\{5\}: Match a exactly 5 times |
\<pattern\> |
word anchoring |
\<root\>: matches root word |
\(\) |
grouping |
|
3. Special characters
special symbols |
describe |
[:alnum:] |
English case and numbers, ie 0-9, az, AZ. ***** |
[:alpha:] |
English upper and lower case, i.e. az, AZ ***** |
[:blank:] |
space bar and tab key |
[:digit:] |
Indicates numbers ***** |
[:lower:] |
Indicates lowercase ***** |
[:upper:] |
Indicates capital ***** |