Previous Table of Contents Next


Regular Expression Anchors

Anchors are used in regular expressions to force the pattern to be considered only if it appears at the beginning or end of the line.

^ Matches the beginning of the line. The ^ (caret) must be the first character of the pattern. If it is not the first character, then it is considered an ordinary character. For example:
^Power tie
matches a line beginning with Power tie.
$ Matches the end of the line. The $ (dollar sign) must be the last character of the pattern. If it is not the last character, then it is considered an ordinary character. For example:
the end\.$
matches a line that ends with the string the end.

Regular Expression Word Anchors

These regular expressions are only available in the ex/vi editor. They match the beginning or end of a word.

\< Matches the beginning of a word. A word is a string consisting only of characters, digits, and underscores. For example:
\<ram
matches ram and ramming, but does not match programming.
\> Matches the end of a word. For example:
the\>
matches the but does not match these.

Grouping Regular Expressions

You can group regular expressions so you can reference them later in the same pattern or in the replacement string of a substitute command. These regular expressions are used by the ed, ex, sed, and grep commands.

\(RE\) Group the regular expression RE for later reference; each RE enclosed in parentheses is referenced by a corresponding number (see \n). The first group is referenced by \1, the second group by \2, and so on. You can have a maximum of nine groups in one regular expression pattern. For example:
\(dimmer\) \(light\) from the \2 \1
matches dimmer light from the light dimmer. A special case for grouping is the ability to find two occurrences of the same string on a line. For example:
^\(.*\)\1$
matches a string such as the the or any other string that is repeated on a single line.
\n Matches the nth grouped \(RE\) within the same regular expression. For example:
\(dog\) \(and\) \(cat\), \3 \2 \1
matches the string dog and cat, cat and dog. The \1 refers to the first string dog, the \2 refers to the second string and, and \3 to the third string cat.

Precise Occurrence Regular Expressions

These regular expressions allow you to match exact numbers of a specified regular expression. These regular expressions are recognized by ed, grep, and sed.

\{m\} Matches exactly m occurrences of the preceding one-character RE. For example:
-\{5\}
matches ----- (exactly five hyphens).
\{m,\} Matches m or more occurrences of the preceding one-character RE. For example:
=\{13,\}
matches thirteen or more consecutive equal signs.
\{m,n\} Matches m through n occurrences of the preceding one-character RE. For example:
z\{2,3\}
matches zz and zzz. It does not match z or zzzz.

FULL REGULAR EXPRESSIONS

Full regular expressions are an extended set of the basic regular expressions, with the exception of the grouping expressions \(RE\). The full regular expressions are only utilized by the nawk and egrep commands.

+ Matches one or more occurrences of the preceding regular expression. For example:
ba+d
matches bad, baad, and so on but does not match bd.
? Matches zero or one occurrence of the preceding regular expression. For example:
ba?d
matches bd and bad but not baad.
| or New-line Matches either one of the regular expressions separated by the | (vertical bar) or a new-line. For example:
I am|you are
matches the string I am or the string you are. The spaces have higher precedence than the |, thus the strings bind together instead of the two words next to the |.
( ) Group a regular expression for precedence control. By using the parentheses you can force the *, +, ?, and | metacharacters to apply to an entire regular expression. For example, consider the following two regular expressions:
John|Paul Jones 
(John|Paul) Jones
The first regular expression matches John or Paul Jones. The second line matches John Jones or Paul Jones.


Previous Table of Contents Next