A.3 Tagged Regular Expressions

Tagged expressions are regular expressions enclosed by the delimiters \ and \ (UNIX) or { and } (non-UNIX). Use tagged expressions to match repeated elements and to mark substrings for use in a replacement. Note that a tagged expression is not the same as a grouped expression.

When you specify a regular expression with tagged subexpressions, PWB finds text that matches the regular expression and marks each substring matching a tagged subexpression.

Example

The UNIX regular expression

\(<\)\([^>]+\)\(>\)

matches the string

<bracketed>

and tags the < , bracketed, and > substrings.

To refer to tagged text in a find or replacement pattern, use \n (UNIX) or $n (non-UNIX), where n is the number of a tagged subexpression from 1 to 9. In a find pattern, this reference matches another occurrence of the previously matched text, not another occurrence of the regular expression. In a replacement, PWB uses the matched text.

The entire match is implicitly tagged for use in replacement text. Use \0 (UNIX) or $0 (non-UNIX) to refer to the entire match. For example, the UNIX find pattern

^\([^ ]+\) +\([^ ]+\).*

with the replace pattern

\2 \1 (\0)

matches lines without leading spaces and at least two words. It replaces them with lines that consist of the transposed words followed by the original line in parentheses.

Example

The tagged expressions:

UNIX Non-UNIX

\([A-Za-z]+\)==\1 {[A-Za-z]+}==$1

match one or more letters followed by two equal signs (==) and a repetition of the letters. They match the first two strings below, but not the third:

ABCxyz==ABCxyz

i==i

ABCxyz==KBCxjj

The following example finds one or more hexadecimal digits followed by the letter H. Each matching string is replaced by a string that consists of the original digits (which were tagged so they could be reused) and the prefix 16#.

1.Find strings of the form hexdigitsH with the UNIX and non-UNIX patterns:

UNIX Non-UNIX

\([0-9a-fA-F]+\)H {[0-9a-fA-F]+}H

These patterns can also be expressed by using the predefined pattern for hexadecimal digits:

UNIX Non-UNIX

\(\:h*\)H {:h}H

2.Replace with the patterns:

UNIX Non-UNIX

16#\1 16#$1