Skip to main content

Regular Expression Examples

DescriptionRegexp (GNU Extended Grep dialect - "grep -E" or "egrep")Matches these linesDoes not match these linesComments
A specific wordHelloHello
Hello there!
Hello, World!
He said, "Hello James", in a very threatening tone
Hi there
Hell of a Day
h el lo
This will match "Hello" anywhere on the line, but not permit any variations, such as spaces in the word or UPPER-/lower-case changes.
A specific word with nothing else on the line^Hello$HelloHello there!
Hello, World!
He said, "Hello James", in a very threatening tone
Hi there
Hell of a Day
h el lo
5-character line^.....$rouge
green
Ho-ho
Yellow
long line
tiny
12-45-78
The anchor characters prevent extra characters from existing between the five characters and the start and end of the line.
Lines that start with a vowel^[AEIOUYaeiouy]Allo
Everything
Energy
Under
Yellow
everything
Hello
White
4164915050
Grinch
The character class includes both UPPERCASE and lowercase letters. You could instead use the option (specific to the tool you're using) to ignore case; for example, ''-i'' for grep or ''/I'' for findstr.
Lines that end in a punctuation mark[[:punct:]]$Hello there!
Thanks.
What do you think?
Hello there
416-491-5050
New Year greetings
Lines where the 3rd character is the letter "J"^..JS J 94723
N J 244325
N3J 2R6
JKLM
Just Kidding
This pattern is anchored to the front of the line, and maches any characters in the first two positions followed by the letter 'J' (uppercase only) in the third position.
Lines that have fields separated by colons, where the third field is the number with four or more digits.^[^:]*:[^:]*:[[:digit:]]{4,}:jdoe:x:1007:1000:J. Doe:/home/jdoe:/usr/bin/bash
rdoe:x:12425:1000:R. Doe:/home/rdoe:/usr/bin/bash
httpd:x:80:80:Apache HTTP server:/dev/null:/bin/falseThe pattern [^:]* will match zero or more characters that are not colons, or in other words, the contents of one field.

If you use this pattern to search the Linux file /etc/passwd, you should see only regular user accounts, since system accounts have user IDs (in the third field) that are three digits or less.
An integer^[-+]?[[:digit:]]+$+15
-2
720
1440
1280
1920
000
012
+ 4
3.14
0x47
$1.13
$4
123,456
This looks for lines that start with a + or - (optional), then contain digits.
A decimal number^[-+]?[[:digit:]]+\.?[[:digit:]]*$+3.14
42
-1000.0
+212
+36.7
42.00
3.333333333
0.976
.976
+-200
1.1.1.1
13.4.7
This will match lines that start with + or - (optional), then contain digits, then optionally contain a decimal point followed by zero or more additional digits.
A Canadian Postal Code^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$H0H 0H0
M3C 1L2
K1A 0A2
T2G 0P3
V8W 9W2
R3B 0N2
M2J2X5
M5S 2C6
POB 1L0
90210
MN4 2R6
A Canadian postal code alternates between letters and digits: A9A 9A9. The first letter must be one of ABCEGHJKLMNPRSTVXY and the remaining letters must be one of ABCEGHJKLMNPRSTVXY.
Phone Numbers (Canada/US)^[^+[:digit:]]*(\+?1)?[^+[:digit:]]*[[2-9]]([^+[:digit:]]*[[0-9]]){9}[^+[:digit:]]*$(416) 967-1111
+1 416-736-3636
416-439-0000
+65 6896 2391
555-1212
A Canadian/US phone number consists of a 3-digit Area Code (which may not start with 0 or 1) and a 10-digit local number consisting of an exchange (3 digits) and a line (4 digits). The country code for Canada and the US is 1, so the number may be preceeded by +1 or 1. Area codes are sometimes contained in parenthesis, and dashes or spaces are sometimes used as separators.
IP Address (IPv4 dotted quad)`^((25[0-5]2[0-4][0-9]1[0-9][0-9][1-9][0-9]
Private IP Address`^(10.(25[0-5]2[0-4][0-9]1[0-9][0-9][1-9][0-9]