Regular Expressions (aka regexes) represent patterns for matching text. Each character in a regular expression is either a metacharacter (having special meaning) or a regular character that has a literal meaning.
The regex a.
is broken down into:
a
is a literal character, which matches the character a
.
is a meta character that matches every character except a newlineThe above regex will match text like: a
, ax
, a0
.
.
is a general pattern that matches everything[a-z]
matches all lower case letters from a
to z
|
to do a Boolean ‘or’ (e.g. gray|grey
matches either gray
or grey
)?
indicates zero or one occurrences of the preceding element (e.g. colou?r
matches color
and colour
)*
indicates zero or more occurrences of the preceding element (e.g. ab*c
matches ac
, abc
, abbc
, abbbc
)+
indicates one or more occurrences of the preceding element (e.g. ab+c
matches abc
, abbc
, abbbc
, but not ac
){n}
indicates the preceding element is matched exactly n times{min,}
indicates the preceding element is matched min or more times{min,max}
indicates the preceding element is matched at least min or more times, but not more than max times.Regex is used programs like grep
(e.g. grep through some logs)
For example, with vim, you can start a search with /
If you want to search for just the character a
by itself, you can use \<
and \>
to run: /\<a\>