Regular Expressions: Positional Anchors

by Mike on May 28, 2009

in Regular Expressions

A  regular expression is a specific text string that describes a search pattern.   These search patterns can be  very complex or very simple, for example if you were looking for “.conf” files in the /etc directory you could use a search with a wildcard that may look like “*.conf”.  That same search as a regular expression would look like this, “.*\.conf$”.  This complexity provides you additional options that you will not have with traditional wildcards.

Regular Expressions, often abbreviated to regexp or regex, can include text strings, or even be a text string, these are literal characters.  They can also include special characters, called metacharacters,  In fact, you can think of regex as a sort of programming language, with text strings as the words and metacharacters as the punctuation.

There are two types of characters that you may use in regular expressions.  Literals are any character which is to be taken literally.  Metacharacters give you the ability to modify your searches according to your needs.  You can, for example, use metacharacters to find a text pattern that occurs at either the beginning or end of a line, while leaving the pattern alone if it occurs somewhere else in the line.  You can also use metacharacters to perform different types of “wildcard” searches.

Position Anchors
Position anchors are used to indicate the location of a word using metacharacters.  The example shows how to find the text string “virtual” either at the start of a line or at the end of a line.
Metacharacter                        Description
^                         This allows you to match a pattern that occurs at the beginning of a line.  You would always place it to the left of the pattern that you want to match.
Example               ^virtual
$                         This allows you to match a pattern that occurs at the end of a line.  You would always place it to the right of the pattern that you want to match.
Example              virtual$

Usage of Positional Anchors
Search with regex in VIM

This usage example  will help you become familiar with the basics of vim and use search patterns. The /etc/syslog file will be used for searching various strings of text at the beginning of a line as well as in between and the end.

Search for Text in Command Mode
Searching for text strings is one of the most important values of vim.  Open your  /etc/syslog.conf and search for the text string “mail”.

vim /etc/syslog.conf

/mail

Now to move to each additional instance of virtual click the “n” key for next.  Move through the entire file. Now search for a new string.  Just enter the command as you will not need to close the last search.  Now modify the search and only look for the string “mail” at the beginning of a line.

/^mail

Try a new search for a text string at the end of a line.

/console$

When you have completed searching exit with:

:q!

Previous post:

Next post: