Regular Expressions

From ben.goodacre.name/tech

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\.)?goodacre.name ben.goodacre.name or goodacre.name
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

Contents

Match a date yyyy-mm-dd

\d{4}-\d{2}-\d{2}

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

Time hh:mm:ss

\d{2}:\d{2}:\d{2}

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

Mac address

With -'s or :'s

[0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f][:-][0-9a-f][0-9a-f]

IP adress

^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))\.([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$

References http://www.analyticsmarket.com/freetools/ipregex

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
Namespaces
Variants
Actions
Navigation
Toolbox