Section 6.3. Metacharacters

6.3. Metacharacters

The characters in Table 6-3 have special meaning only in search patterns .

Table 6-3. Metacharacters used in search patterns

Character

Pattern

.

Match any single character except newline. Can match newline in awk .

*

Match any number (or none) of the single character that immediately precedes it. The preceding character can also be a regular expression; e.g., since . (dot) means any character, .* means match any number of any character.

^

Match the following regular expression at the beginning of the line or string.

$

Match the preceding regular expression at the end of the line or string.

[ ]

Match any one of the enclosed characters. A hyphen (-) indicates a range of consecutive characters. A circumflex (^) as the first character in the brackets reverses the sense of the character set, so that it matches any one character not in the list. A hyphen or close bracket (]) as the first character is treated as a member of the list. All other metacharacters are treated as members of the list (i.e., literally).

{ n , m }

Match a range of occurrences of the single character that immediately precedes it. The preceding character can also be a metacharacter. { n } matches exactly n occurrences, { n ,} matches at least n occurrences, and { n , m } matches any number of occurrences between n and m . n and m must be between 0 and 255, inclusive.

\{ n , m \}

Just like { n , m }, above, but with backslashes in front of the braces.

 

Turn off the special meaning of the character that follows .

\( \)

Save the pattern enclosed between \( and \) into a special holding space. Up to nine patterns can be saved on a single line. The text matched by the subpatterns can be replayed in substitutions by the escape sequences \1 to \9.

\n

Replay the n th subpattern enclosed in \( and \) into the pattern at this point. n is a number from 1 to 9, with 1 starting on the left. See "Examples of Searching" later in this chapter.

\< \>

Match characters at beginning (\<) or end (\>) of a word.

+

Match one or more instances of preceding regular expression.

?

Match zero or one instances of preceding regular expression.

Match the regular expression specified before or after.

( )

Apply a match to the enclosed group of regular expressions.


Many Unix systems allow the use of POSIX character classes within the square brackets that enclose a group of characters. These classes, listed in Table 6-4, are typed enclosed in [: and :] . For example, [[:alnum:]] matches a single alphanumeric character.

Table 6-4. Character classes

Class

Characters matched

alnum

Alphanumeric characters

alpha

Alphabetic characters

blank

Space or tab

cntrl

Control characters

digit

Decimal digits

graph

Nonspace characters

lower

Lowercase characters

print

Printable characters

space

Whitespace characters

upper

Uppercase characters

Xdigit

Hexadecimal digits


The characters in Table 6-5 have special meaning only in replacement patterns .

Table 6-5. Metacharacters used in replacement patterns

Character

Pattern

\

Turn off the special meaning of the character that follows.

\ n

Restore the text matched by the n th pattern previously saved by \( and \). n is a number from 1 to 9, with 1 starting on the left.

&

Reuse the text matched by the search pattern as part of the replacement pattern.

~

Reuse the previous replacement pattern in the current replacement pattern. Must be the only character in the replacement pattern. ( ex and vi )

%

Reuse the previous replacement pattern in the current replacement pattern. Must be the only character in the replacement pattern. ( ed )

\u

Convert first character of replacement pattern to uppercase.

\U

Convert entire replacement pattern to uppercase.

\l

Convert first character of replacement pattern to lowercase.

\L

Convert entire replacement pattern to lowercase.

\e, \E

Turn off previous \u, \U, \l, and \L.




MAC OS X Tiger in a Nutshell
Mac OS X Tiger in a Nutshell: A Desktop Quick Reference (In a Nutshell (OReilly))
ISBN: 0596009437
EAN: 2147483647
Year: 2003
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net