Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
RegexBuddy—Better than a regular expression reference!

Regular Expression Reference: Character Classes

FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
Character class [ When used outside a character class, [ begins a character class. Inside a character class, different rules apply. Unless otherwise noted, the syntax on this page is only valid inside character classes, while the syntax on all other reference pages is not valid inside character classes. YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYES
Literal character Any character except ^-]\ All characters except the listed special characters are literal characters that add themselves to the character class. [abc] matches a, b or c YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYES
Backslash escapes a metacharacter \ (backslash) followed by any of ^-]\ A backslash escapes special characters to suppress their special meaning. [\^\]] matches ^ or ] YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMAECMA
awk
YESnononononoYESYES
Literal backslash \ A backslash is a literal character that adds a backslash to the character class. [\] matches \ nonononononononononononononobasic
extended
grep
egrep
awk
basic 1.38–1.63
extended 1.38–1.65
grep 1.38–1.65
egrep 1.38–1.65
noYESYESYESYESYESnono
Range - (hyphen) between two tokens that each specify a single character. Adds a range of characters to the character class. [a-zA-Z0-9] matches any ASCII letter or digit YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYES
Negated character class ^ (caret) immediately after the opening [ Negates the character class, causing it to match a single character not listed in the character class. [^a-d] matches x (any character except a, b, c or d) YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYES
Literal opening bracket [ An opening square bracket is a literal character that adds an opening square bracket to the character class. [ab[cd]ef] matches aef], bef], [ef], cef], and def] YESYESnoYESYESYESYESYESYESYESYESYESYESnoYESYESYESYESYESYESYESYESnono
Nested character class [ An opening square bracket inside a character class begins a nested character class. [ab[cd]ef] is the same as [abcdef] and matches any letter between a and f. nonoYESnononononononononono1.9nononononononononono
Character class subtraction [base-[subtract]] Removes all characters in the "subtract" class from the "base" class. [a-z-[aeiuo]] matches a single letter that is not a vowel. YESnon‑ECMA 2.0–4.6
ECMA 2.0–4.7
nonononononononononononononononononononoYESYES
Character class intersection [base&&[intersect]] Reduces the character class to the characters present in both "base" and "intersect". [a-z&&[^aeiuo]] matches a single letter that is not a vowel. V2noYESnononononononononono1.9nononononononononono
Character class intersection [base&&intersect] Reduces the character class to the characters present in both "base" and "intersect". [\p{Nd}&&\p{InThai}] matches a single Thai digit. nonoYESnononononononononono1.9nononononononononono
Character escape \n, \r and \t Add an LF character, a CR character, or a tab character to the character class, respectively. [\n\r\t] a line feed, a carriage return, or a tab. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
awk
ECMA
awk
YESstringstringstringstringnoYESYES
Character escape \a Add the "alert" or "bell" control character (ASCII 0x07) to the character class. [\a\t] matches a bell or a tab character. YESYESYESYESYESYESYESYESYESnononoYESYESawkECMA
awk
YESnonononononono
Character escape \b Add the "backspace" control character (ASCII 0x08) to the character class. [\b\t] matches a backspace or a tab character. YESYESnoYESYESYESYESYESYESYESYESYESYESYESECMA VC’12–VC’15
awk VC’08–VC’17
ECMA
awk
YESnononononoYESYES
Character escape \B Add a backslash to the character class. [\B] matches \ nonononononononononononononononoYESnonononononono
Character escape \e Add the "escape" control character (ASCII 0x1A) to the character class. [\e\t] matches an escape or a tab character. YESYESYESYESYESYESYESYESYESnonononoYESnoECMA
awk
YESnonononononono
Character escape \f Add the "form feed" control character (ASCII 0x0C) to the character class. [\f\t] matches a form feed or a tab character. YESYESYESYESYESYESYESYESYESYESYESYESYESYESECMA
awk
ECMA
awk
YESnonononononono
Character escape \v Add the "vertical tab" control character (ASCII 0x0B) to the character class, without adding any other vertical whitespace. [\v\t] matches a vertical tab or a tab character. V1 onlyYES4–7nonononononoYESYESYESYESYESECMA
awk
ECMA
awk
YESnonononononono
POSIX class [:alpha:] Matches one character from a POSIX character class. Can only be used in a bracket expression. [[:digit:][:lower:]] matches one of 0 through 9 or a through z ASCIInonoUnicodeASCIIASCII5.3.4 Unicode
5.0.0 code page
ASCIIcode pagenononono1.9 Unicode
1.8 ASCII
UnicodeUnicodeUnicodeASCIIASCIIASCIIASCIIUnicodenono
POSIX class [:^alpha:] Matches one character that is not part of a specific POSIX character class. Can only be used in a bracket expression. [5[:^digit:]] matches the digit 5 or any other character that is not a digit. nononoYESYESYESYESYESYESnononono1.9errorYESerrorerrorerrorerrorerrorerrornono
POSIX shorthand class [:d:], [:s:], [:w:] Matches one character from the POSIX character classes "digit", "space", or "word". Can only be used in a bracket expression. [[:s:][:d:]] matches a space, a tab, a line break, or one of 0 through 9 V2 ASCIInononononononononononononoUnicodeUnicodenononononononono
POSIX shorthand class [:l:] and [:u:] Matches one character from the POSIX character classes "lower" or "upper". Can only be used in a bracket expression. [[:u:]][[:l:]] matches Aa but not aA. V2 ASCIInonononononononononononononoUnicodenononononononono
POSIX shorthand class [:h:] Matches one character from the POSIX character classes "blank". Can only be used in a bracket expression. [[:h:]] matches a space. V2 ASCIInonononononononononononononoECMA 1.42–1.65
basic 1.42–1.63
extended 1.42–1.65
grep 1.42–1.65
egrep 1.42–1.65
awk 1.42–1.65
Unicode
nononononononono
POSIX shorthand class [:V:] Matches a vertical whitespace character. Can only be used in a bracket expression. [[:v:]] match any single vertical whitespace character. V2 ASCIInonononononononononononononoECMA 1.42–1.65
basic 1.42–1.63
extended 1.42–1.65
grep 1.42–1.65
egrep 1.42–1.65
awk 1.42–1.65
Unicode
nononononononono
POSIX class Any supported \p{…} syntax \p{…} syntax can be used inside character classes. [\p{Digit}\p{Lower}] matches one of 0 through 9 or a through z YESn/an/aYESn/an/an/an/an/an/an/an/an/a1.9n/aextended
egrep
n/an/an/an/an/an/an/an/a
POSIX class \p{Alpha} Matches one character from a POSIX character class. \p{Digit} matches any single digit. UnicodenoASCIIUnicodenonononononononono1.9 UnicodenoECMA
extended
egrep
awk
Unicode
nononononononono
POSIX class \p{IsAlpha} Matches one character from a POSIX character class. \p{IsDigit} matches any single digit. UnicodenoASCIIUnicodenononononononononononononononononononono
POSIX collation sequence [.span-ll.] Matches a POSIX collation sequence. Can only be used in a bracket expression. [[.span-ll.]] matches ll in the Spanish locale errornonoerrorerrorerrorerrorerrorerrornononono1.8 only errorfailYESYESYESYESYESYESYESnono
POSIX character equivalence [=x=] Matches a POSIX character equivalence. Can only be used in a bracket expression. [[=e=]] matches e, é, è and ê in the French locale errornonoerrorerrorerrorerrorerrorerrornononono1.8 only errorYESYESYESYESYESYESYESYESnono
FeatureSyntaxDescriptionExample JGsoft .NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath

Make a Donation

Did this website just save you a trip to the bookstore? Please make a donation to support this site, and you'll get a lifetime of advertisement-free access to this site! Credit cards, PayPal, and Bitcoin gladly accepted.

Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Shorthands
Anchors
Word Boundaries
Quantifiers
Unicode
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Mode Modifiers
Recursion & Balancing Groups
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
Conditionals
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog