0% found this document useful (0 votes)
394 views

Regular Expressions Cheat Sheet

This document provides a cheat sheet for regular expressions with syntax examples to match patterns in text. It includes expressions to match word characters (\w), non-word characters (\W), whitespace (\s), and capture groups to extract matches. The cheat sheet also demonstrates how to match alternatives, reference previous captures, and use character classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
394 views

Regular Expressions Cheat Sheet

This document provides a cheat sheet for regular expressions with syntax examples to match patterns in text. It includes expressions to match word characters (\w), non-word characters (\W), whitespace (\s), and capture groups to extract matches. The cheat sheet also demonstrates how to match alternatives, reference previous captures, and use character classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

(?

<nam > )




e x create a named capture (?< i s > d)(?
f r t \ Match : 1325
 2

\ D

match a non-digi t

\D

The 5 cats ate
 52
 grou p

<sc nd> d) d*


ro \ \ first : 1
 hell o


Example 

12 Angry men

Example 

10032

Example 
 Example 
 Example 



: 3
Example 


non-matches -
Syntax Description Syntax Description second
pattern matches pattern matches non matches

\ w

match word character s

\wee\w

s

tree The bee
 ( |y)


x match several ( | a)


re b re d
 rant


bee4
eels eat meat
alternative pattern s
banter
bear

Regular Expressions Cheat Sheet


\ W

match non-word \Wbat\W

At bat
 wombat
 \n



reference previous ( )( w*) 1




b \ \ blob
 bea r

character s
Swing the bat fast
bat 53
captures where n is the bribe


bring



1


Learn regular expressions online at www DataCamp com . . group index starting at

\ s

match whitespace

\sfox\s

the fox ate
 ’
it s the fox .

his fox ran
foxfur

\k<nam > e reference named (?< i s >5)


f r t 51245
 523

\ S

match non-whitespace

\See\S

trees
 the bee stun g
 captures ( d*) k< i s >
\ \ f r t 55 51
beef
The tall tre e

What is a regular expression?

Regular expression (regex or regexp) is a pattern of characters that describes an amount of text. To process
\metacharacter escape a metacharacter

to match on the
\.

\^
The cat ate

2^3
.
 the cat ate


23
> Lookahead
regexes, you will use a “regex engine.” Each of these engines use slightly different syntax called regex metacharacter
You can specify that specific characters must appear before or after you match, without including those
flavor. A list of popular engines can be found here. Two common programming languages we discuss on
characters in the match.
DataCamp are Python and R which each have their own engines.

Character
Example 
 Example 
 Example 

Since regex describes patterns of text, it can be used to check for the existence of patterns in a text,
> classes
Syntax Description
pattern matches -
non matches

extract substrings from longer strings, and help make adjustments to text. Regex can be very simple to

describe specific words, or it can be more advanced to find vague patterns of characters like the top-level (?= )



x looks ahead at the next an(?=an)
 banan a
 ban d

domain in a url.
Character classes are sets or ranges of characters.
characters without using iss(?=ipp)


Mississippi


missed



them in the match


Example 
 Example 
 Example 



Syntax Description
pattern matches non matches -

[x y]

match several character s

gr[ea]y

gray
 green
 (?! )


x looks ahead at next ai(?!n)

fail
 faint

> Definitions grey
gree k
characters to not match brail

trai n


on

Literal character: A literal character is the most basic regular expression you can use. It simply matches
[x -y]

match a range of [a-e]

am ber
 fo x

the actual character you write. So if you are trying to represent an “r,” you would write r.
character s
brand
join
(?<= )



x looks at previous
(?<= )a





tr
trail
 bea r

characters for a match translat e


streak



Metacharacter: Metacharacters signify to the regex engine that the following character has a special
[^x y]

does not match several gr[^ea]y

green
 gray
 without using those in

meaning. You typically include a \ in front of the metacharacter and they can do things like signify the
character s
greek
grey

the match

beginning of a line, end of a line, or to match any single character.

[\^ -] match metacharacters 4[\^\.-+*/]\d

4^3
 44
 (?<! ) x looks at previous (?! )a


tr bea r
 trail

Character class: A character class (or character set) tells the engine to look for one of a list of characters.

It is signified by [ and ] with the characters you are looking for in the middle of the brackets.

inside the character 4.2 23 characters to not match translate strained

on
class

Capture group: A capture group is signified by opening and closing, round parenthesis. They allow you to

group regexes together to apply other regex features like quantifiers (see below) to the group.

> R epetition
> Literal matches and modifiers
Modifiers are settings that change the way the matching rules work.

> Anchors Rather than matching single instances of characters, you can match repeated characters.

Example 
 Example 
 Example 


Example 
 Example 
 Example 



Syntax Description
pattern matches -
non matches

Anchors match a position before or after other characters.


Syntax Description
pattern matches non matches -
\Qx\E

match start to finish

\Qtell\E
 tell
 ’
I ll tell you this


Example 
 Example 
 Example 
 x *



match z ero or more ar*o

caca o
 arugul a
 \Q\ d\E
\d
I have 5 coins

Syntax Description
pattern matches -
non matches
times
carrot
artichok e

^

match start of line

^r

rabbit
 parrot

(?i) (?-i).


x set the regex string to (?i) (?-i)


te sTep
 Trench


raccoon
ferret

x +

match one or more time s

re +

green 
 trap
 case-insensitiv e
tEach
bear

tre e
ruine d

$

match end of line

t$

rabbit
 trap

(? ) (?- )


x x x regex ignores (? ) a p(?- )


x t x tap
 c a t


foot
star

x ?

match z ero or one time s

ro ?a

roast
 root
 whitespac e
tapdanc e
rot a potato

rant
rear

\A

match start of line

\Ar

rabbit
 parrot

(?s) (?-s)






x turns on single-line/ (?s) i s and


f r t first an d
 first an d

raccoon
ferret

x {m}

match m time s

\we{2}\w

dee r
 re d
 DOTALL mode which s c nd(?-s)
e o Secon d an d third




 second 

see r
enter
makes the “.” include and hi d




t r and third




new-line symbols (\n) in


\Z

match end of line

t\Z

rabbit
 trap

x {m,}

match m or more time s

2{3,}4

671 2224

- 224
 addition to everything

foot
star

2222224
123
else

(?m) (?-m) ^ and $ to be at and d


\b


match characters at the \bfox\b


the red fox ran
 foxtrot

x {m,n}

match between m and n 12{1,3}3

1234
 15335
 x changes ^e eat an sleep
 treat and

sleep$
start or end of a word

the fox ate

foxskin scarf


times
1222384
1222223
end of line rather than eat an d
 sleep


end of string sleep eat and sleep.

\B match characters in the \Bee\B trees
 bee



x *?, x +?, 
 match the minimum re +? tree
 trout


de
middle of other non- beef tree
et c. number of times - known freeeee roasted

space characters
as a la z
y quantifier
> Unico

Capturing, alternation &b k


Regular expressions can work beyond the Roman alphabet, with things like Chinese characters or emoji.

> Matching types of character > ac references


Co de Points: The hexadecimal number used to represent an abstract character in a system like unicode.

Graphemes: Is either a codepoint or a character. All characters are made up of one or more graphemes
In order to extract specific parts of a string, you can capture those parts, and even name the parts that you
Rather than matching specific characters, you can match specific types of characters such as letters,
in a sequence.
captured.
numbers, and more.

Example 
 Example 
 Example 
 Example 
 Example 
 Example 



Example 
 Example 
 Example 

Syntax Description
pattern matches -
non matches
Syntax Description
pattern matches non matches - Syntax Description
pattern matches -
non matches

.

anything except for a c.e

clean
 acert

( )


x capturing a patter n

(iss)+

Mississipp i
 mist
 \X

match grapheme s

\u0000 gmail

@gmail
 gmail


linebrea k
chea p
cent

misse d
persist
www.email@gmail
@aol

\ d

match a digi t

\d

6060-842
 tw o
 (?: )

x create a group without (?:a )(cd)


b Match: abcd
 acbd



\X\X match special characters \u00e8 or è e

2b|^2b
**___
capturin g
Group 1: cd
like ones with an accent \u006 5\u0 300

\ D

match a non-digi t

\D

The 5 cats ate
 52
 (?<nam > )


e x create a named capture (?< i s > d)(?
f r t \ Match : 1325
 2

12 Angry men
10032
p

<sc nd> d) d*

: 1
 o


. .
grou ro \ \ first hell

second : 3
Learn Data Skills Online at www DataCamp com

\ w

match word character s
\wee\w

s

tree The bee


bee4
eels eat meat

( |y)


x match several ( | a)


re b re d
 rant


match non-word
alternative pattern s
banter
bear

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy