Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Regular Expressions Quick Reference

RSS
Modified on Mon, Feb 13, 2012, 12:52 PM by Administrator Categorized as (Favorites), Quick Reference, Regular Expressions
{outline||Section <1> - }

Special Characters

CharacterUsage
[ starts a character class
\ cancels the special meaning of the next character
^ (caret) start-of-string anchor
$ end-of-string anchor
. Matches any character except for line breaks
| (pipe) alternation group
? indicates the previous character or group is optional. Equivalent to {0,1}
* (asterisk)-
+ -
( -
) -

Literal Characters

TextDescription
\fForm feed (ASCII 12)
\n Line feed (ASCII 10)
\r Carriage return (ASCII 13)
\t Tab (ASCII 9)
\v Vertical tab (ASCII 11)
\a Alarm (ASCII 7)
\e Escape (ASCII 25)
\xxx The ASCII character specified by the octal number xxx
\xnn The ASCII character specified by the hexadecimal number nn
\cX The control character Control-X. For example, \cI is equivalent to \t and \cJ is equivalent to \n.

Character Classes

A character class matches one out of a set of characters.

TextDescription
[...] Any one character between the brackets.
[^...]Any one character not between the brackets.
. Any character except newline. Equivalent to [^\n]
\w Any word character. Equivalent to [a-zA-Z0-9_] and [[:alnum:]_]
\W Any non-word character. Equivalent to [^a-zA-Z0-9_] and [^[:alnum:]_]
\s Any whitespace character. Equivalent to [ \t\n\r\f\v] and [[:space:]]
\S Any non-whitespace. Equivalent to [^ \t\n\r\f\v] and [^[:space:]] Note: \w != \S
\d Any digit. Equivalent to [0-9] and [[:digit:]]
\D Any character other than a digit. Equivalent to [^0-9] and [^[:digit:]]
[\b] A literal backspace (special case)
[[:class:]] class can be one of the following: alnum, alpha, ascii, blank, cntrl, digit, graph, lower, print, punct, space upper, xdigit

Replacement

TextDescription
\ Turn off the special meaning of the following character.
\n Restore the text matched by the nth 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).

Anchors

TextDescription
^ (caret)matches start of string
$matches end of string
\bmatches word boundary (a position between a character that can be matched by \w and one that cannot
\Bnegation of \b
\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.

Repetition

PatternDescription
{n,m} Match the previous item at least n times but no more than m times.
{n,} Match the previous item n or more times.
{n} Match exactly n occurrences of the previous item.
? Match zero or one occurrences of the previous item. Equivalent to {0,1}
+ Match one or more occurrences of the previous item. Equivalent to {1,}
* Match zero or more occurrences of the previous item. Equivalent to {0,}
{}? Non-greedy match - will not include the next match's characters.
?? Non-greedy match.
+? Non-greedy match.
*? Non-greedy match. For example, in ^(.*?)\s*$ the grouped expression will not include trailing spaces.

Options

TextDescription
g Perform a global match. That is, find all matches rather than stopping after the first match.
i Do case-insensitive pattern matching.
m Treat string as multiple lines (^ and $ match internal \n).
s Treat string as single line (^ and $ ignore \n, but . matches \n).
x Extend your pattern's legibility with whitespace and comments.

Look-Around and Advanced Expressions

PatternDescription
(?#abc)Comment; abc is ignored.
(?:abc) Matches but doesn't return abc
(?=abc)Matches if expression would match abc next. This is called a non-consuming search, and can be used to match several search terms independent of what order they appear.
(?!abc) Matches if expression wouldn't match abc next
(?imsx) Change matching rules (see options) midway through an expression.

Grouping

PatternDescription
(...) Grouping. Group several items into a single unit that can be used with *, +, ?, |, and so on, and remember the characters that match this group for use with later references.
(?<name>...) Named group
| Alternation. Match either the subexpressions to the left or the subexpression to the right.
\n Match the same characters that were matched when group number n was first matched. Groups are subexpressions within (possibly nested) parentheses.
^ (caret) Match the beginning of the string, and, in multiline searches, the beginning of a line.
$ Match the end of the string, and, in multiline searches, the end of a line.
\b Match a word boundary. That is, match the position between a \w character and a \W character. (Note, however, that [\b] matches backspace.)
\B Match a position that is not a word boundary.

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.