Regular Expressions Cheat Sheet

A quick reference guide for regular expressions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started.

3,543,619 views 648,911 downloads 19 Oct 2011 History
4.8 avg · 371 ratings

Anchors

^
Start of string, or start of line in multi-line pattern
\A
Start of string
$
End of string, or end of line in multi-line pattern
\Z
End of string
\b
Word boundary
\B
Not word boundary
\<
Start of word
\>
End of word

Character Classes

\c
Control character
\s
White space
\S
Not white space
\d
Digit
\D
Not digit
\w
Word
\W
Not word
\x
Hexade­cimal digit
\O
Octal digit

POSIX

[:upper:]
Upper case letters
[:lower:]
Lower case letters
[:alpha:]
All letters
[:alnum:]
Digits and letters
[:digit:]
Digits
[:xdigit:]
Hexade­cimal digits
[:punct:]
Punctu­ation
[:blank:]
Space and tab
[:space:]
Blank characters
[:cntrl:]
Control characters
[:graph:]
Printed characters
[:print:]
Printed characters and spaces
[:word:]
Digits, letters and underscore

Assertions

?=
Lookahead assertion
?!
Negative lookahead
?<=
Lookbehind assertion
?!= or ?<!
Negative lookbehind
?>
Once-only Subexp­ression
?()
Condition [if then]
?()|
Condition [if then else]
?#
Comment

Quanti­fiers

*
0 or more
{3}
Exactly 3
+
1 or more
{3,}
3 or more
?
0 or 1
{3,5}
3, 4 or 5
Add a ? to a quantifier to make it ungreedy.

Escape Sequences

\
Escape following character
\Q
Begin literal sequence
\E
End literal sequence
"­Esc­api­ng" is a way of treating characters which have a special meaning in regular expres­sions literally, rather than as special charac­ters.

Common Metach­ara­cters

^
[
.
$
{
*
(
\
+
)
|
?
<
>
The escape character is usually \

Special Characters

\n
New line
\r
Carriage return
\t
Tab
\v
Vertical tab
\f
Form feed
\xxx
Octal character xxx
\xhh
Hex character hh

Groups and Ranges

.
Any character except new line (\n)
(a|b)
a or b
(...)
Group
(?:...)
Passive (non-c­apt­uring) group
[abc]
Range (a or b or c)
[^abc]
Not (a or b or c)
[a-q]
Lower case letter from a to q
[A-Q]
Upper case letter from A to Q
[0-7]
Digit from 0 to 7
\x
Group/­sub­pattern number "­x"
Ranges are inclusive.

Pattern Modifiers

g
Global match
i *
Case-i­nse­nsitive
m *
Multiple lines
s *
Treat string as single line
x *
Allow comments and whitespace in pattern
e *
Evaluate replac­ement
U *
Ungreedy pattern
* PCRE modifier

String Replac­ement

$n
nth non-pa­ssive group
$2
"­xyz­" in /^(abc­(xy­z))$/
$1
"­xyz­" in /^(?:a­bc)­(xyz)$/
$`
Before matched string
$'
After matched string
$+
Last matched string
$&
Entire matched string
Some regex implem­ent­ations use \ instead of $.

Badges

Essential Cheat Sheet Famous Cheat Sheet Loved Cheat Sheet Useful Cheat Sheet Great Cheat Sheet Liked Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Appreciated Cheat Sheet Downloaded from Unusual Locations × 41

Comments (78)

Edwin_04 17:35 21 Jun 2026

That's great!

Reply
Edwin_04 19:19 21 Jun 2026

yes

Reply
Edwin_04 19:20 21 Jun 2026

ofc

Reply
jmitchell 19:16 22 Mar 2024

Thank you !

Reply
aliaksandr 22:49 29 Jan 2021

Lazy quantifiers Syntax => Description ?? => Lazy zero or one (optional) +? => Lazy one or more *? => Lazy zero or more {n}? => Lazy n {n,}? => Lazy n or more {m,n}? => Lazy m,n

Reply
aliaksandr 04:49 27 Jan 2021

Syntax => Description (?<name>…) => A named group (?name…) => Another named group (?P<name>…) => A named group in Python \k<name> => Reference by name in Perl \k'name' => Reference by name in Perl \g{name} => Reference by name in Perl \k{name} => Reference by name in .NET (?P=name) => Reference by name in Python

Reply
aliaksandr 04:03 27 Jan 2021

(?d) => Unix lines => Java (?i) => Case insensitive => PCRE, Perl, Java (?J) => Allow duplicate names => PCRE* (?m) => Multiline => PCRE, Perl, Java (?s) => Single line (dotall) => PCRE, Perl, Java (?u) => Unicode case => Java (?U) => Default match lazy => PCRE (?x) => Ignore whitespace, comments => PCRE, Perl, Java (?-…) => Unset or turn off options => PCRE

Reply
aliaksandr 04:02 27 Jan 2021

with \t separation: (?d) Unix lines Java (?i) Case insensitive PCRE, Perl, Java (?J) Allow duplicate names PCRE* (?m) Multiline PCRE, Perl, Java (?s) Single line (dotall) PCRE, Perl, Java (?u) Unicode case Java (?U) Default match lazy PCRE (?x) Ignore whitespace, comments PCRE, Perl, Java (?-…) Unset or turn off options PCRE

Reply
aliaksandr 03:50 26 Jan 2021

\cJ = \n = U+000A = LF = Line feed (newline, end of line) \cM = \r = U+000D = CR = Carriage return Windows line endings (\r\n also called CRLF) Linux/Unix line endings (\n also called LF);

Reply
aliaksandr 03:19 24 Jan 2021

i think, need to create a regex for libreoffice

Reply
aliaksandr 03:17 24 Jan 2021

(...) Group replace with: (...) Capturing group a capturing group. Then you’ll refer to the content of the group with a backreference. To create a capturing group, enclose a \d in a pair of parentheses

Reply
Cheatography101 20:14 18 May 2020

can anybody please help me on how to "edit" (save and continue later) and "delete" (erase) DRAFT cheat sheets?

Reply
fipnova51 07:23 19 Mar 2018

Thanks for this valuable document

Reply
Chris 09:19 7 Jun 2012

David, Regex is programming language neutral, as in, it doesn't matter if you are programming regex expressions in javascript, c#, c++, PHP, or even command line *nix, makes no difference. Only thing you have to watch out for is some programming languages may require different various regex characters to be escaped differently (so the programming language doesn't try to interpret it). Usually a backslash. For instance \\ means ONE backslash in many languages.

Reply
BillSmith 20:27 30 Jan 2018

Chris, I am a bit confused. At the following URL (https://www.regular-expressions.info/cookbook.html), I just read this: "The book covers the regular expression flavors .NET, Java, JavaScript, XRegExp, Perl, PCRE, Python, and Ruby, and the programming languages C#, Java, JavaScript, Perl, PHP, Python, Ruby, and VB.NET. After a quick introduction, the book starts with a detailed regular expressions tutorial which equally covers all 8 regex flavors." I am having trouble squaring the quoted statement with what you wrote above. Thanks.

Reply
BillSmith 20:14 30 Jan 2018

Greetings, and thanks for providing this. Where can I find a comprehensive, accessible textual reference on this topic that includes numerous examples? Instead of pursuing blind trial and error, I would like to understand thoroughly what I am doing, and why. Thanks.

Reply
sainojin 07:15 27 Nov 2017

Can you talk about flag in Regex, sir? Specially, it is flag Y. And it will be great if there is examples. Anyway, thank you so much.

Reply
hatelove 02:02 6 Jan 2017

better clarify which syntax flavor this cheatsheet is about, is it BRE? ERE or PCRE?

Reply
[deleted] 15:12 27 Oct 2016

Cảm ơn | Thanks!

Reply
Culbin 17:00 12 Jul 2016

Hi Folks! I use regexp syntax to schedule TV recordings via TVHeadend which is case-insensitive by default. Here's an extreme example which highlights the problem. Simply using 'big brother' produces many different program name matches like: Big Brother Big Brother's Bit On The Side Big Brother: Annihilation Big Brother: Eviction Massacre New: Big Brother New: Big Brother's Bit On The Side New: Big Brother: Live... I'd like to tell it to ignore the Bit On The Side programs but match the rest. I've researched till I'm blue in the face with no luck. My tiny brain tells me that in regular English it would read like this: match 'big brother' but not if contains 'bit on the side' but I'm lost when it comes to translating this to regexp syntax. I honestly don't know if it accepts Lookahead or Lookbehind which I see is mentioned a lot, sorry. Any geniuses out there got any ideas? Much appreciated.

Reply
Abdel Maghraby 17:19 28 Mar 2016

Hello Guys, I am trying to create a code to prevent white spaces before or after a string. [ name ] [ name] [name ] Ideally I want this to be strictly [Name] How can i achieve that?

Reply
tasjaevan 08:24 26 Mar 2016

Hi Dave - could you an entry for free whitespace regexes using the ?x syntax?

Reply
brent 15:28 5 Mar 2016

Do you have any issue with us distributing the PDF regex cheat sheet as is with no changes to others. Before I put it on our internal collaboration tool I need to make sure there are no issues from you in doing so. I will not be modifying the PDF or removing your details from the sheet, it will be just as it is but shareable from within our company's portal.

Reply
DaveChild 15:31 5 Mar 2016

It's released (as all cheat sheets here are) under a CC license, so you can redistribute it according to the terms here: http://creativecommons.org/licenses/by-nc-sa/2.0/uk/ So, go for it :)

Reply
Bartleby 15:29 5 Mar 2016

Pradeep: I was able to accomplish what you're looking for with the following text (I'm using Notepad++): Search: (\))(,) Replace: \1\r\n\2

Reply
Bartleby 15:29 5 Mar 2016

Shrirang: What're you trying to do with the date? Just capture it? I don't know how detailed you want to be, but this'll capture everything in what you posted. For experienced people it's probably really inelegant, but it's functional, anyway: (\[)(\d{2})(\-)([A-Z]+)(\-)(\d{4})(\:\d{2}\:\d{2}\:\d{2})(\s+)(\-\d+)(\])

Reply
pradeep 13:29 24 Feb 2016

I need to split a group of elment baased on "," delimiter this is the example input :"(10,{10,9,8,7,6,5,4,3,2,1}),(8,{8,7,6,5,4,3,2,1}),(8,{8,7,6,5,4,3,2,1}) output: (10,{10,9,8,7,6,5,4,3,2,1}) (8,{8,7,6,5,4,3,2,1}) (8,{8,7,6,5,4,3,2,1}) regex: [()], which is not getting me in the way i want.

Reply
Shrirang Garge 13:29 24 Feb 2016

I am finding it difficult to write a regex for the date input..it looks like this...[31-Mar-2015:06:22:48 -600]. I am trying with [0-9a-zA-Z) but giving me null values. Please help.

Reply
Ish 21:27 26 Jan 2016

Very helpful. Thanks.

Reply
DZanke 14:16 28 Nov 2015

Great list! I'm new to Teradata Regular Expressions and couldn't find them anywhere.

Reply
Philbo Baggins 14:45 7 Nov 2015

Hi I am a techno retard I gather Regex is coding flavour. However I have no idea what you write is there sny resources. Sorry for stupidity. Philbo.

Reply
Prabhakaran Govindaraj 10:59 7 Oct 2015

Thanks for the nice and comprehensive resource.

Reply
Shamim 09:11 14 Sep 2015

Thanks for the great job!

Reply
romeoh 08:56 5 Aug 2015

good

Reply
matu 17:45 29 Jun 2015

If you like this you may want to check out my tutorial on how to use regular expressions in Ruby :) http://www.blackbytes.info/2015/06/mastering-ruby-regex/

Reply
ms_jo553698 17:25 20 Jun 2015

I have been searching for programs that others might be using to roll dice in Pachisi on the internet. I know nothing about programming and don't know how to word the question. Are there cheat sheets out there for something like this?

Reply
Sudhakar 18:36 25 May 2015

Hi Team, How to write a regular expression for this kind of below line present in document . Ref: 000/SP/00-000 I need to set Target data formats and Keywords for this field. Thanks in advance.

Reply
TME520 03:31 23 Apr 2015

Nice cheat sheet, great job !

Reply
Hemant Bellani 12:26 29 Mar 2015

Thank you for this !!

Reply
Jeff 08:50 11 Mar 2015

Great Cheatsheet. Maybe you could add the toggles like (?i...), (?-i...), (?i: ...), (?-i: ...) and their cousins with "m" and "x". THanks for the great work

Reply
Ken Sanders 09:12 5 Nov 2014

Hello Jaya, MySQL supports regular expressions: select distinct col_1 from tablename where col_1 REGEXP '[a-z]{3}-[a-z]{3}-5'

Reply
jaya prakash 09:35 31 Oct 2014

Hi , Can u help me to find regular expression -- SELECT distinct col_1 FROM tablename WHERE (SUBSTR(col_1,-1,1)) = '5' need resuslt as abc-cxy-5 not as abc-cxy-65 not as abc-cxy-555 not as abc-cxy-05545

Reply
Aaron 11:13 8 Sep 2014

Ted, when the PDF displays in Chrome, right-click in the page and select Save As.

Reply
William 09:21 10 Jul 2014

I agree with Roedy Green. I came here looking for specific a version of regex. This is still so helpful.

Reply
Ted 16:34 13 Jun 2014

Can you please fix the pdf so it is able to download? Right now my browser (Google Chrome) only show a raw pdf instead of downloading it. Thank you very much :)

Reply
Roedy Green 20:57 26 May 2014

There are so many dialects of regex. The most important fact should be right up top, which dialects do you cover?

Reply
yashawanth 10:24 17 May 2014

what is mean by (.*?) please let me know as soon as possible ??

Reply
Bhaggs 11:33 21 Apr 2014

Does these support ereg too?

Reply
Mervin 13:37 19 Apr 2014

Thank you. It would have been better if you would have included test, match, etc.

Reply
Sahana A V 17:48 17 Feb 2014

Thank you for the Regex cheat sheet :-)

Reply
Simon 17:33 17 Feb 2014

This is a great cheat-sheet. Two minor niggles: * Would be great to hint on the characters hidden in the character classes (\s = [ \t\n\r\f], \d = [0-9], \w = [a-zA-Z_0-9]) * I think possibly there's a mistake in the section "Special Characters" - \xxx is probably not the octal character xxx. See "Character Classes": it should be \Oxxx (and by the way: why are \O and \x duplicated in "Special Characters" and "Character Classes"...) And I support Edir's request for a section "Case Conversion". But again: great sheet, thanks!

Reply
Benoit 08:54 14 Feb 2014

It would be great to increase in some ways the --> : <---- in the "(?:...) Passive (non-c­apt­uring) group" description. Just after the "?", it is praticaly not visible. Thanks!

Reply
David 15:14 13 Feb 2014

\x is a term in "Regular Expressions Character Classes" for an hexadecimal digit. How does this compare to the \xhh "Special Characters"? Is it supported today?

Reply
David 15:14 13 Feb 2014

Is \x (Regular Expressions Character Classes) supported anywhere? Is this a new class that has just been added, because I am unable to use it in working with IPV6 addresses. Searching for a string containing something like 2001::1a79 with a RegEx 2001::\x{1,4} will fail, but if I use 2001::[a-fA-F0-9]{1,4} will work.

Reply
David 15:14 13 Feb 2014

Is \x supported anywhere? I can't seem to find where it is supported. E.g., perl on MacOS 10.7.5. Searching for on-line examples or help also fails, in that no one knows about it.

Reply
Drew White 15:10 13 Feb 2014

I'm trying to come up with a regex string to filter results to a directory that includes a-zA-Z but that also includes an underscore ('_'). Do you know of a way to do this?

Reply
david.baird 18:59 15 Jul 2013

Is \x supported anywhere? I can't find examples of it in use searching the web. It also does not work in a script on my Macintosh, OSX 10.7.5 using the OS's perl installation.

Reply
Gabe 17:16 26 Mar 2013

I have a database using regex. I am trying to use ^file to get all files with name file_,file ,file_name_date. but it is not working any help

Reply
Edir 09:17 15 Feb 2013

Could be added to the list. Case Conversion \l Make next character lowercase \u Make next character uppercase \L Make entire string (up to \E) lowercase \U Make entire string (up to \E) uppercase \u\L Capitalize first char, lowercase rest (sentence)

Reply
Bill 11:48 24 Jan 2013

Hey Dave. Thanks for the cheat sheet. You may want to change "Not a or b or c" when you describe the [^abc} negated character class, because in English, the negation is ambiguous. It could mean "neither a nor be nor c." Or the "a" could be the only negated disjunct. You could mean (~a v (b v c)).

Reply
Rob 11:47 24 Jan 2013

Is there a reason why the ']' character is not listed under metacharacters? Doesn't that character require to be escaped if searched for?

Reply
ty 12:50 22 Nov 2012

Nice sheet.

Reply
fsnow55 16:17 22 Oct 2012

I was confused by the first comment (which was wrong, but you compounded the error with an acknowlegement). ^ is the start of string or line. Period. Also, your cheat sheet is better organized than the more comprehensive http://www.regular-expressions.info/ since its more succinct. The latter has a 1-page summary but its too verbose.

Reply
Travis 13:07 4 Oct 2012

@david, this cheat sheet is pretty neutral. The most common flavor is Perl Compatible Regular Expressions (PCRE). Javascript's engine is close to that and PHP also has Perl Compatible functions for Regex; they use the PREG prefix. Most everything on this sheet should be supported by PHP's engine (I think POSIX character classes are not). Javascript's engine isn't as featureful. Some advanced features aren't supported, but all the basics are there. If you need a multiline match and you can't use the flag, you can use an inverted class range such as [\s\S] in place of the . (dot) to match anything including newlines. @Chilean+kris w, You need to find a resource for learning Regular Expressions. This cheat sheet is for reference, not learning. Check out http://www.regular-expressions.info/

Reply
kris w 08:50 13 Sep 2012

Is there a cheat sheet to the cheat sheet? Is this in plain english anywhere?..."negative lookahead"..huh?

Reply
Chilean 14:03 23 Aug 2012

Hi, I'm trying to learn REGEX, and I need to find this: "Page 1 Of 60", ..... "Page 50 of 60", But I can't find it using reg. expressions! :(. How would you do that? Thank you!

Reply
littleguy 15:23 6 Aug 2012

Great and useful stuff!

Reply
Andy Grosland 09:45 28 Jun 2012

Very handy, thank you! Don't forget Perl ;-)

Reply
david 08:58 20 May 2012

What language/flavor is this? Or I should also ask, if non is specified, what tends to be the default? I'm specifically looking for php or javascript, and I know they're all mostly the same, but not 100%. Thanks!

Reply
Jeff 15:44 9 May 2012

Great resource! Thanks for putting this together and sharing.

Reply
Jorge 12:16 4 May 2012

It would be nice to see the list of white space characters

Reply
Doug 22:52 12 Apr 2012

Would you add \Q... \E to the cheatsheet?

Reply
Doug 10:02 28 Nov 2011

Your regex cheatsheet says ^ is "Start of string" and $ is "End of string"

Reply
DaveChild 10:02 28 Nov 2011

Hi Doug. I've clarified that section. Thanks for the heads up :)

Reply
DaveChild 10:02 28 Nov 2011

No worries Keith - easy mistake to make!

Reply
Anonymous 10:02 28 Nov 2011

It looks unchanged to me. ^ still says it's "start of string" and $ still says "end of string". They should be reversed, right?

Reply
Anonymous 10:02 28 Nov 2011

Actually, I'm sorry, you're right! I was pretty confused there, sorry if I've confused anyone else.

Reply
Tom Hunter 10:02 28 Nov 2011

Can you tag this as 'regex'? Searching for regex on cheatography yields two other results, but not this one.

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.