Regular expression to find words/strings not ending with a character

Have a dream? Start learning your way toward it with courses from $12.99. Shop now for extra savings1px

If you wish to find strings at the end of a line that don't end with a particular character, you can use a bracket expresion with a caret character after the left square bracket in a regular expression.

Metacharacter Description
[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed.

E.g., suppose I have file named list.txt containing the following words:



coat
cop
core
coaster
clam
chrome
counter
cure
claustrophobia
closet

If I wanted to find only those lines that don't end with either the letter "e", "r", or "t", I could use the regular expression [^ert]$ for a grep search as shown below:

$ grep '[^ert]$' list.txt
cop
clam
claustrophobia
$

The dollar sign at the end of the regular express indicates that I'm looking for the pattern at the end of the line.

Metacharacter Description
$ Matches the ending position of the string or the position just before a string-ending newline. In line-based tools, it matches the ending position of any line.

I receive a spreadsheet every month containing over a thousand email addresses that I need to validate. I extract the column containing the email addresses from the spreadsheet to a text file with a Python script. Most of the email addresses end with .com, though there are also .gov and .net addresses. Occasionally the "m" has been mistyped as an "n", so I can search for any email addresses that have some other character after ".co" in the Vi text editor with the regular expression /\.co[^m]\n. The backslash before the period is needed to "escape" the normal metacharacter meaning of the period, which otherwise in a regular expression (regexp) represents any character. The \n represents the newline character, i.e., the end of the line. I could also type a slash followed by \.[^m]\$ in Vi, since the dollar sign represents the end of the line. For a grep search, though, I need to use the dollar sign at the end of the regular expression.

$ grep '\.co[^m]$' email_list.txt
john.doe@example.con
$