There isn't just one set of regular expressions. Different applications use different types of regular expression engines. These are the
two most commong regular expression engines:
Basic Regular Expressions
^
The caret symbol will match the patern or character searched at the begining of each line.
Example:
$
The dolar sign will match the patern or character searched at the end of each line.
Example:
[ ]
Brackets can be used to specify a set of characters like [ioe]. Grep will find all the lines that contain at least one of the specified characters inside the brackets.
Example:
The caret symbol inside brackets [^] will invert the selection. Grep will match all the lines that DO NOT contain the specified characters.
Example:
You can use a range of characters within brackets like [a-zAZ] or numbers like [0-9].
Example:
\
Back slash is the escape character. It used when the searced pattern includes special characters.
Example:
.
The period is used to match any character in the place where it is used.
Example:
*
The asterix can be a little bit tricky. It is NOT a wildcard, it will match zero or more times the preceding character.
Example:
Character classes
It is possible to use ranges like [0-9] or a class like [[:alpha:]] to match for only numbers.
The BRE contains special character classes that can be used to match specific types of characters.
| BRE Special Character Classes. |
| [[:alpha:]] |
Match any alphabetical character, either upper or lower case. |
| [[:alnum:]] |
Match any alphanumeric character 0 - 9, A - Z, or a - z. |
| [[:blank:]] |
Match a space or Tab character. |
| [[:digit:]] |
Match a numerical digit from 0 through 9. |
| [[:lower:]] |
Match any lower-case alphabetical character a - z. |
| [[:print:]] |
Match any printable character. |
| [[:punct:]] |
Match a punctuation character. |
| [[:space:]] |
Match any whitespace character: space, Tab, NL, FF, VT, CR. |
| [[:upper:]] |
Match any upper-case alphabetical character A - Z. |
Extended Regular Expressions
?
The question mark indicates that the preceding character can appear zero or one time.
The difference with the asterix is that the question mark will not match repeating occurrences of the character.
Example:
+
The plus sign indicates that the preceding character can appear one or more times, but must be present at least once.
{ }
Curly braces allow to spedify a limit on a repeatable expression,{lower,upper} or just {min}.
Example:
|
The pipe symbol is used to match two or more patterns treated as a logical OR formula.
( )
Parentheses are used to group regular expressions patterns, the resulting group will be treated as a character.
Example:
Examples
Find all blanc lines in a file:
grep '^$' {file}
Find any two digit numbers in a file:
grep ^[0-9][0-9]$ {file}
Find any line that starts with a period:
grep '^\.' {file}
Find lines that only have one character:
grep '^.$' {file}
© 2025 Julian's Corner. All rights reserved