Regular Expression Examples
| Description | Regexp (GNU Extended Grep dialect - "grep -E" or "egrep") | Matches these lines | Does not match these lines | Comments |
|---|---|---|---|---|
| A specific word | Hello | HelloHello there!Hello, World!He said, "Hello James", in a very threatening tone | Hi thereHell of a Dayh 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$ | Hello | Hello there!Hello, World!He said, "Hello James", in a very threatening toneHi thereHell of a Dayh el lo | |
| 5-character line | ^.....$ | rougegreenHo-ho | Yellowlong linetiny12-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] | AlloEverythingEnergyUnderYelloweverything | HelloWhite4164915050Grinch | 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 there416-491-5050New Year greetings | |
| Lines where the 3rd character is the letter "J" | ^..J | S J 94723N J 244325N3J 2R6 | JKLMJust 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/bashrdoe:x:12425:1000:R. Doe:/home/rdoe:/usr/bin/bash | httpd:x:80:80:Apache HTTP server:/dev/null:/bin/false | The 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-2720144012801920000012 | + 43.140x47$1.13$4123,456 | This looks for lines that start with a + or - (optional), then contain digits. |
| A decimal number | ^[-+]?[[:digit:]]+\.?[[:digit:]]*$ | +3.1442-1000.0+212+36.742.003.3333333330.976 | .976+-2001.1.1.113.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 0H0M3C 1L2K1A 0A2T2G 0P3V8W 9W2R3B 0N2M2J2X5M5S 2C6 | POB 1L090210MN4 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-3636416-439-0000 | +65 6896 2391555-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] |