Metacharacters

A metacharacter is a character that has a special meaning during pattern processing. You use metacharacters in regular expressions to define the search criteria and any text manipulations.

Search string metacharacters are different from replacement string metacharacters.

Search string metacharacters

The following table lists metacharacters for extended regular expression searches.

If you use basic regular expressions, not all metacharacters are supported. The function of the backslash character is reversed. You must include a backslash character before all metacharacters.

Table 1. Search string metacharacters
Metacharacter Action
^ Beginning of line
$ End of line
| Or

Not applicable to basic regular expressions.

[abc] Match any character enclosed in the brackets
[^abc] Match any character not enclosed in the brackets
[a-z] Match the range of characters specified by the hyphen
[:cclass:] Use the character list that is specified by cclass:
  • alnum = Uppercase and lowercase alphabetic characters and numbers: [A-Za-z0-9]
  • alpha = Uppercase and lowercase alphabetic characters: [A-Za-z]
  • blank = Whitespace and tab characters
  • cntrl = Control characters
  • digit = Numbers: [0-9]
  • graph = Visible characters (the alnum class plus the punct class)
  • lower = Lowercase alphabetic characters: [a-z]
  • print = Printable characters (the graph class plus whitespace)
  • punct = Punctuation marks: !"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~
  • space = Whitespace characters: tab, newline, carriage-return, form-feed, and vertical-tab
  • upper = Uppercase alphabetic characters: [A-Z]
  • xdigit = Hexadecimal characters: [0-9a-fA-F]

These classes are valid for single-byte character sets.

[=cname=] Substitute the character name that is specified by cname with the corresponding character code.

For a list of character names, see Regex character names.

. Match any single character.
( ) Group the regular expression within the parentheses.
? Match zero or one of the preceding expression.

Not applicable to basic regular expressions.

* Match zero, one, or many of the preceding expression.
+ Match one or many of the preceding expression.

Not applicable to basic regular expressions.

\ Use the literal meaning of the metacharacter.

For basic regular expressions, treat the next character as a metacharacter.

Replacement string metacharacters

The following table lists metacharacters for extended regular expression searches.

If you use basic regular expressions, not all metacharacters are supported. The function of the backslash character is reversed. You must include a backslash character before all metacharacters.

Table 2. Replacement string metacharacters
Metacharacter Action
& Reference the entire matched text for string substitution.

For example, the statement execute function regex_replace('abcdefg', '[af]', '.&.') replaces 'a' with '.a.' and 'f' with '.f.' to return: '.a.bcde.f.g'.

\n Reference the subgroup n within the matched text, where n is an integer 0-9.

\0 and & have identical actions.

\1 - \9 substitute the corresponding subgroup.

For example, the statement execute function regex_replace('abcdefg', '[af]', '.\0.') replaces 'a' with '.a.' and 'f' with '.f.' to return: '.a.bcde.f.g'.

For example, the statement execute function regex_replace('abcdefg', '([af])([bg])', '.p1-\1.p2-\2.') replaces 'ab' with '.p1-a.p2-b' and 'fg' with '.p1-f.p2-g.' to return: '.p1-a.p2-b.cde.p1-f.p2-g.'.

Not applicable to basic regular expressions.

\ Use the literal meaning of the metacharacter, for example, \& escapes the Ampersand symbol and \\ escapes the backslash.

For basic regular expressions, treat the next character as a metacharacter.