Regular Expressions Quick Reference

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Use

[set]

To match any character


In that set

[^set]
[az]
[^az]
.
\char

Not in that set


In the a-z range
Not in the a-z range
Any except \n (new line)
Escaped special character

Use
\t
\v
\b
\e
\r
\f
\n
\a
\c char

To match
Horizontal tab
Vertical tab
Backspace
Escape
Carriage return
Form feed
New line
Bell (alarm)
ASCII control
character

Unicode
\u0009
\u000B
\u0008
\u001B
\u000D
\u000C
\u000A
\u0007

Use
\octal
\x hex
\u hex

To match character with


2-3 digit octal character code
2-digit hex character code
4-digit hex character code

Use
To match character
\p{ctgry} In that Unicode category or block
\P{ctgry} Not in that Unicode category or
block
\w
Word character
\W
Non-word character
\d
Decimal digit
\D
Not a decimal digit
\s
White-space character
\S
Non-white-space char

Use
(exp)
(?<name>exp)
(?<name1name2>exp)
(?:exp)
(?=exp)

(?>exp)

Noncapturing group
Zero-width positive
lookahead
Zero-width negative
lookahead
Zero-width positive
lookbehind
Zero-width negative
lookbehind
Non-backtracking (greedy)

Option
i
m
n
s
x

Effect on match
Case-insensitive
Multiline mode
Explicit (named)
Single-line mode
Ignore white space

Use
(?imnsximnsx)
(?imnsximnsx:exp)

To
Set or disable the specified
options
Set or disable the specified
options within the
expression

(?!exp)
(?<=exp)

Greedy
*
+
?
{n}
{n,}
{n,m}

Use
^
\A
\z
\Z
$
\G
\b
\B

Lazy
*?
+?
??
{n}?
{n,}?
{n,m}?

Matches
0 or more times
1 or more times
0 or 1 time
Exactly n times
At least n times
From n to m times

To specify position
At start of string or line
At start of string
At end of string
At end (or before \n at end) of string
At end (or before \n at end) of string
or line
Where previous match ended
On word boundary
Not on word boundary

To define
Indexed group
Named group
Balancing group

(?<!exp)

June 2014
2014 Microsoft. All rights reserved.

Use
\n
\k<name>

To match
Indexed group
Named group

Use
a |b
(?(exp)
yes | no)
(?(name)
yes | no)

To match
Either a or b
yes if exp is matched
no if exp isn't matched
yes if name is matched
no if name isn't matched

Use
$n

$$
$&
$`
$'
$+
$_

To substitute
Substring matched by group
number n
Substring matched by group
name
Literal $ character
Copy of whole match
Text before the match
Text after the match
Last captured group
Entire input string

Use
(?# comment)
#

To
Add inline comment
Add x-mode comment

${name}

For detailed information and examples, see


http://aka.ms/regex
To test your regular expressions, see
http://regexlib.com/RETester.aspx

Category
Lu
LI
Lt
Lm
Lo
L
Mn
Mc
Me
M
Nd
Nl
No
N
Pc
Pd
Ps
Pe
Pi
Pf
Po
P
Sm
Sc
Sk
So
S
Zs
Zl
Zp
Z
Cc
Cf
Cs
Co
Cn
C

Description
Letter, uppercase
Letter, lowercase
Letter, title case
Letter, modifier
Letter, other
Letter, all
Mark, nonspacing combining
Mark, spacing combining
Mark, enclosing combining
Mark, all diacritic
Number, decimal digit
Number, letterlike
Number, other
Number, all
Punctuation, connector
Punctuation, dash
Punctuation, opening mark
Punctuation, closing mark
Punctuation, initial quote mark
Puntuation, final quote mark
Punctuation, other
Punctuation, all
Symbol, math
Symbol, currency
Symbol, modifier
Symbol, other
Symbol, all
Separator, space
Separator, line
Separator, paragraph
Separator, all
Control code
Format control character
Surrogate code point
Private-use character
Unassigned
Control characters, all

For named character set blocks (e.g., Cyrillic), search


for "supported named blocks" in the MSDN Library.

Class: System.Text.RegularExpressions.Regex

Pattern matching with Regex objects


To initialize with Use constructor
Regular exp
Regex(String)
+ options
Regex(String, RegexOptions)
+ time-out
Regex(String, RegexOptions,
TimeSpan)
Pattern matching with static methods
Use an overload of a method below to supply the
regular expression and the text you want to search.

Finding and replacing matched patterns


To
Use method
Validate match Regex.IsMatch
Retrieve single
Regex.Match (first)
match
Match.NextMatch (next)
Retrieve all
Regex.Matches
matches
Replace match Regex.Replace
Divide text
Regex.Split
Handle char
Regex.Escape
escapes
Regex.Unescape
Getting info about regular expression patterns
To get
Use Regex API
Group names
GetGroupNames
GetGroupNameFromNumber
Group numbers GetGroupNumbers
GetGroupNumberFromName
Expression
ToString
Options
Options
Time-out
MatchTimeOut
Cache size
CacheSize
Direction
RightToLeft

You might also like