[Chapter 6] 6.3 Metacharacters
Author: Daniel Gilly & the Staff of O'Reilly & Associates
Date: 1998-08-06T21:59:45Z

#

| Chapter 6 Pattern Matching |

## 6.3 Metacharacters

The characters below have special meaning only in search patterns:

. | Match any single character except newline.
* | 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.
$ | Match the preceding regular expression at the end of the line.
[ ] | 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: 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.
`\{``n`,`m``\}` | Match a range of occurrences of the single character that immediately precedes it. The preceding character can also be a regular expression. \{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 256, inclusive.
\ | 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. They can be "replayed" in substitutions by the escape sequences \1 to \9.
`\< \>` | 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.

The characters below have special meaning only in replacement patterns.

\ | Turn off the special meaning of the character that follows.
\`n` | Restore the nth pattern previously saved by \( and \). n is a number from 1 t 9, with 1 starting on the left.
& | Reuse the search pattern as part of the replacement pattern.
~ | Reuse the previous replacement pattern in the current replacement pattern.
`\u` | Convert first character of replacement pattern to uppercase.
`\U` | Convert replacement pattern to uppercase.
`\l` | Convert first character of replacement pattern to lowercase.
`\L` | Convert replacement pattern to lowercase.

6.2 Metacharacters, Listed by UNIX Program |  | 6.4 Examples of Searching