Regular Expressions


Jump to: navigation, search

Regular expressions and used in Perl, Apache Rewrite, Vim among many other uses.

Regex Meaning
. Any single character except a newline
^ The beginning of the line or string
$ The end of the line or string
* Zero or more of the last character
+ One or more of the last character
 ? Zero or one of the last character
(ben\.)? or
t.e t followed by any 1 single character followed by e. This will match the , tre , tle , but not te or tale
^f f at the beginning of a line
^ftp ftp at the beginning of a line
e$ e at the end of a line
und* un followed by zero or more d characters. This will match un , und , undd , unddd etc.
.* Any string without a newline. This is because the . matches anything except a newline and the * means zero or more of these.
^$ A line with nothing in it.
[qjk]Either q or j or k
[^qjk]Neither q nor j nor k
[a-z]Anything from a to z inclusive
[^a-z]No lower case letters
[a-zA-Z]Any letter
[a-z]+Any non-zero sequence of lower case letters
[^qjk]+ Any non-zero sequence that does not contain a q nor j nor k.
jelly|creamEither jelly or cream
le)gsEither eggs or legs
(da)+Either da or dada or dadada or...
\nA newline
\tA tab
\wAny alphanumeric (word) character.
The same as [a-zA-Z0-9_]
\WAny non-word character.
The same as [^a-zA-Z0-9_]
\dAny digit. The same as [0-9]
\DAny non-digit. The same as [^0-9]
\sAny whitespace character: space, tab, newline, etc
\SAny non-whitespace character
\bA word boundary, outside [] only
\BNo word boundary


Match a date yyyy-mm-dd


Needs some work, will also match 9999-99-99 .

Time hh:mm:ss


Needs some work, will also match 99:99:99 .

Mac address

With -'s or :'s


IP adress



Shorter but will also show 999.999.999.999:
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' 
Personal tools