Create
Learn
Share

Regular Expressions

rename
Updated 2006-10-28 00:38

Summary

This shows the basic characters used in regular expressions.

Basic Syntax

CharactersDescription
. (period)Any single character except line breaks (\r and \n). (Most regex flavors have an option to make the dot match line break characters too.)
^ (caret)Start of string. (Matches a position rather than a character. Most regex flavors have an option to make the caret match after line breaks (i.e. at the start of a line in a file) as well.)
$ (dollar)End of string. (Matches a position rather than a character. Most regex flavors have an option to make the dollar match before line breaks (i.e. at the end of a line in a file) as well. Also matches before the very last line break if the string ends with a line break.)
\AStart of string the regex pattern is applied to. (Matches a position rather than a character. Never matches after line breaks.)
\ZEnd of string the regex pattern is applied to. (Matches a position rather than a character. Never matches before line breaks, except for the very last line break if the string ends with a line break.)
\zMatches at the end of the string the regex pattern is applied to. (Matches a position rather than a character. Never matches before line breaks.)
\bWord boundary. (Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters.)
memorize

Basic Syntax

CharactersDescription
\BMatches at the position between two word characters. ((i.e the position between \w\w) as well as at the position between two non-word characters (i.e. \W\W).)
? (Question Mark)Makes the preceding item optional. (Greedy, so the optional item is included in the match if possible.)
* (star)Repeats the previous item zero or more times. (Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all.)
*? (lazy star)Repeats the previous item zero or more times. (Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item.)
+ (plus)Repeats the previous item once or more. <small>Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once.
{n} (where n is an integer >= 1)Repeats the previous item exactly n times.
{n,m} (where n >= 1 and m >= n)Repeats the previous item between n and m times. (Greedy, so repeating m times is tried before reducing the repetition to n times.)
{n,m}? (where n >= 1 and m >= n)Repeats the previous item between n and m times. (Lazy, so repeating n times is tried before increasing the repetition to m times.)
memorize