Thursday, August 04, 2011

Metacharacters and Regular Expressions

A regular expression is a pattern of text that consists of ordinary characters (for example, letters a through z) and special characters, known as metacharacters. The pattern describes one or more strings to match when searching a body of text. The regular expression serves as a template for matching a character pattern to the string being searched.

The following table contains the list of some metacharacters and their behavior in the context of regular expressions:

[Character] - [Description]

  • " \ " - Marks the next character as either a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".
  • " ^ " - Matches the position at the beginning of the input string.
  • " $ " - Matches the position at the end of the input string.
  • " * " - Matches the preceding subexpression zero or more times.
  • " + " - Matches the preceding subexpression one or more times.
  • " ? " - Matches the preceding subexpression zero or one time.
  • " {n} " - Matches exactly n times, where n is a nonnegative integer.
  • " {n,} " - Matches at least n times, n is a nonnegative integer.
  • " {n,m} " - Matches at least n and at most m times, where m and n are nonnegative integers and n <= m.
  • " ? " - When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible.
  • " . " - Matches any single character except "\n".
  • " x|y " - Matches either x or y.
  • " [xyz] " - A character set. Matches any one of the enclosed characters.
  • " [^xyz] " - A negative character set. Matches any character not enclosed.
  • " [a-z] " - A range of characters. Matches any character in the specified range.
  • " [^a-z] " - A negative range characters. Matches any character not in the specified range.
  • " \b " - Matches a word boundary, that is, the position between a word and a space.
  • " \B " - Matches a nonword boundary. 'er\B' matches the 'er' in "verb" but not the 'er' in "never".
  • " \d " - Matches a digit character.
  • " \D " - Matches a nondigit character.
  • " \f " - Matches a form-feed character.
  • " \n " - Matches a newline character.
  • " \r " - Matches a carriage return character.
  • " \s " - Matches any whitespace character including space, tab, form-feed, etc.
  • " \S " - Matches any non-whitespace character.
  • " \t " - Matches a tab character.
  • " \v " - Matches a vertical tab character.
  • " \w " - Matches any word character including underscore.
  • " \W " - Matches any nonword character.
  • " \un " - Matches n, where n is a Unicode character expressed as four hexadecimal digits. For example, \u00A9 matches the copyright symbol (©).

reference: http://www.macronimous.com/resources/writing_regular_expression_with_php.asp