Regular Expression Cheat Sheet

Image

Regular Expression Cheat Sheet

This article will help you with regular expression cheat sheet . Refer this article while implementing patterns and syntax.
Blog Author
Published on
Mar 10, 2023
Views
4526
Read Time
15 Mins
Table of Content

A specialised characteristic pattern which describes a certain amount of text is referred to as Regex or regexp (Regular expressions). When these patterns are used from any source it is called Regex Cheat sheet or regular expressions cheat sheet. In natural processing and various other tasks like common text data manipulation 

More on regular expressions cheat sheet

There are several regex engines that can be used to process regexes. Regex engines act differently depending on the syntax they use, and you can get a list of most popular engines in this blog. Two of the common languages include Python and R, which have their own engines. 

A regex or regular expression cheat sheet can be used to extract substrings from lower strings, check for patterns in a text, and modify text since it describes patterns of text. There are many different types of regex, including simple regex which describes specific words, and more complex regex which finds vague patterns of characters, such as the top-level domain at the end of a URL.

Definitions

  • Literal Character - You can use literal characters in regular expressions to represent actual characters. For example, if you want to represent an ”r”, then you would write “r”.
  • Metacharacter - A metacharacter is a character that tells the regex engine that the following character has a unique meaning(usually after /). Metacharacters can serve as markers for beginning, ending or matching single characters.
  • Character Class -  In a character class(or character set ), the engine is instructed exactly what characters it should look for. It is denoted by [ and ], with the characters you wish to find between the brackets.
  • Capture Group - It allows you to group regexes together using round parenthesis (see below) and apply other regex features to the group such as quantifiers.

Data Science

Certification Course

100% Placement Guarantee

View course

Regular expressions cheat sheets are a powerful tool for manipulating text and searching for patterns. Here's a cheat sheet of some of the most commonly used syntax and regex Python:

1. Anchors

These match a position just before or after any other character:

SyntaxDescriptionExample PatternExample MatchesExample non- matches
^, \AMatches the start of line^r, \Ar

run

recess 

dog

cat

$, \ZMatch the end of linen$, n\Z

corn

fun

mobile

speaker

\bMatch the characters at the start and end of a word\brat\b

the rat ran

the rat ate

ratskin 

ratflow

\BMatch characters in between of a word\Boo \B

book

shook

shoe

flue

 

 

 

2. Matching types of character

The more specific type of character tp match is not just the character itself, but the type of character, such as letter, number, and more.

SyntaxDescriptionExample PatternExample MatchesExample non- matches
.Anything except for a line breakc.e

cheap

clean

 

Acert

cent

\dmatch a digit\d

6060-896

2b|^2b

Tw 

**___

\DMatch a non-digit\DThe 5 cats ate 12 angry rats

52

10032

\w

 

Match word characters\wee\w

Trees

bees

The bee

Eels eat skin

\WMatch non-word characters\Wbat\W

At bat

Swing the bat slow

Wombat

bat53

\sMatch whitespace\sfox\s

The fox ate

The fox ran

It’s the fox

fox-fur

\SMatch non-whitespace\See\S

Trees

beef

The bee stung

The tall tree

\metacharacterEscape a metacharacter to match on the metacharacter

\.

\^

The cat ate.

2^3

The cat ate 

23

 

3.  Character classes

These are sets or ranges of characters

SyntaxDescriptionExample PatternExample MatchesExample non-matches
[xy]Match several charactersgr[ea]y

Gray

grey

Green

greek

[x-y]Match a range of characters[a-e]

Amber

brand

Fox 

join

[^xy]Does not match several charactersgr[^ea]y

Green

greek

Gray

grey

[\ ^-]Match metacharacters inside the character class4[\^\.-+*/]\d

4^3

4.2

44

23

 

4.  Repetition

The repeated appearance of characters can be matched rather than a single instance of them.

SyntaxDescriptionExample PatternExample MatchesExample non- matches
x*Matches zero or more timesar*o

Cacao

carrot

Arugula

artichoke

x+Matches one or more timesre+

Green

tree

Trap

ruined

x?Matches zero or one timesro?a

roast

rant

Root

rear

x{m}Match m times\we{2}\w

deer

seer

Red

enter

x{m,}Match m or more times2{3,}4

671-2224

22224

224

123

x{m,n}Match between m and n times12{1,3}3

1234

1222389

15335

122223

x*?,x+?, etcmatch the minimum number of times - known as a lazy quantifierre+?

Tree

free

Trout

roasted

 

5. Capturing, alternation & backreferences

Using the capture function, you can identify the parts of the string that you want to extract.

SyntaxDescriptionExample PatternExample MatchesExample non-matches
(x)Capturing a pattern(iss)+

Mississippi

missed

Mist 

persist

(?:x)Create a group without capturing(?:ab)(cd)

Match: abcd

Group 1: cd

acbd
(?<name>X)Create a named capture group(?<first>\d)(?<scrond>\d)\d*

Match: 1325

first: 1

second: 3

2

hello

(x|y)Match several alternative patterns(re|ba)

red

banter

Rant 

bear

\nreference previous captures where n is the group index starting at 1(b)(\w*)\1

blob

bribe

Bear

bring

\k<name>Reference named captures(?<first>5)(\d*)\k<first>

51245

55

523

51

 

6. Lookahead

Characters can be specified before or after matching, without those characters appearing in the match.

SyntaxDescriptionExample PatternExample MatchesExample non-matches
(?=x)looks ahead at the next characters without using them in the match

an(?=an)

iss(?=ipp)

banana

Mississippi

band

missed

(?!x)looks ahead at next characters to not match onai(?!n)

fail

brail

faint

train

(?<=x)looks at previous characters for a match without using those in the match(?<=tr)a

trail

translate

bear

streak

(?<!x)looks at previous characters to not match on(?!tr)a

bear

translate

trail

strained

 

7. Literal matches and modifiers

Changing the matching rules using modifiers changes how they work.

Syntax

Description

Example PatternExample Matches

Example non- mathes

\Qx\E

A specialised characteristic pattern which describes a certain amount of text is referred to as Regex or regexp (Regular expressions). When these patterns are used from any source it is called Regex Cheat sheet or regular expressions cheat sheet. In natural processing and various other tasks like common text data manipulation 

More on regular expressions cheat sheet

There are several regex engines that can be used to process regexes. Regex engines act differently depending on the syntax they use, and you can get a list of most popular engines in this blog. Two of the common languages include Python and R, which have their own engines. 

A regex or regular expression cheat sheet can be used to extract substrings from lower strings, check for patterns in a text, and modify text since it describes patterns of text. There are many different types of regex, including simple regex which describes specific words, and more complex regex which finds vague patterns of characters, such as the top-level domain at the end of a URL.

Definitions

  • Literal Character - You can use literal characters in regular expressions to represent actual characters. For example, if you want to represent an ”r”, then you would write “r”.
  • Metacharacter - A metacharacter is a character that tells the regex engine that the following character has a unique meaning(usually after /). Metacharacters can serve as markers for beginning, ending or matching single characters.
  • Character Class -  In a character class(or character set ), the engine is instructed exactly what characters it should look for. It is denoted by [ and ], with the characters you wish to find between the brackets.
  • Capture Group - It allows you to group regexes together using round parenthesis (see below) and apply other regex features to the group such as quantifiers.

Data Science

Certification Course

100% Placement Guarantee

View course

Regular expressions cheat sheets are a powerful tool for manipulating text and searching for patterns. Here's a cheat sheet of some of the most commonly used syntax and regex Python:

1. Anchors

These match a position just before or after any other character:

SyntaxDescriptionExample PatternExample MatchesExample non- matches
^, \AMatches the start of line^r, \Ar

run

recess 

dog

cat

$, \ZMatch the end of linen$, n\Z

corn

fun

mobile

speaker

\bMatch the characters at the start and end of a word\brat\b

the rat ran

the rat ate

ratskin 

ratflow

\BMatch characters in between of a word\Boo \B

book

shook

shoe

flue

 

 

 

2. Matching types of character

The more specific type of character tp match is not just the character itself, but the type of character, such as letter, number, and more.

SyntaxDescriptionExample PatternExample MatchesExample non- matches
.Anything except for a line breakc.e

cheap

clean

 

Acert

cent

\dmatch a digit\d

6060-896

2b|^2b

Tw 

**___

\DMatch a non-digit\DThe 5 cats ate 12 angry rats

52

10032

\w

 

Match word characters\wee\w

Trees

bees

The bee

Eels eat skin

\WMatch non-word characters\Wbat\W

At bat

Swing the bat slow

Wombat

bat53

\sMatch whitespace\sfox\s

The fox ate

The fox ran

It’s the fox

fox-fur

\SMatch non-whitespace\See\S

Trees

beef

The bee stung

The tall tree

\metacharacterEscape a metacharacter to match on the metacharacter

\.

\^

The cat ate.

2^3

The cat ate 

23

 

3.  Character classes

These are sets or ranges of characters

SyntaxDescriptionExample PatternExample MatchesExample non-matches
[xy]Match several charactersgr[ea]y

Gray

grey

Green

greek

[x-y]Match a range of characters[a-e]

Amber

brand

Fox 

join

[^xy]Does not match several charactersgr[^ea]y

Green

greek

Gray

grey

[\ ^-]Match metacharacters inside the character class4[\^\.-+*/]\d

4^3

4.2

44

23

 

4.  Repetition

The repeated appearance of characters can be matched rather than a single instance of them.

SyntaxDescriptionExample PatternExample MatchesExample non- matches
x*Matches zero or more timesar*o

Cacao

carrot

Arugula

artichoke

x+Matches one or more timesre+

Green

tree

Trap

ruined

x?Matches zero or one timesro?a

roast

rant

Root

rear

x{m}Match m times\we{2}\w

deer

seer

Red

enter

x{m,}Match m or more times2{3,}4

671-2224

22224

224

123

x{m,n}Match between m and n times12{1,3}3

1234

1222389

15335

122223

x*?,x+?, etcmatch the minimum number of times - known as a lazy quantifierre+?

Tree

free

Trout

roasted

 

5. Capturing, alternation & backreferences

Using the capture function, you can identify the parts of the string that you want to extract.

SyntaxDescriptionExample PatternExample MatchesExample non-matches
(x)Capturing a pattern(iss)+

Mississippi

missed

Mist 

persist

(?:x)Create a group without capturing(?:ab)(cd)

Match: abcd

Group 1: cd

acbd
(?<name>X)Create a named capture group(?<first>\d)(?<scrond>\d)\d*

Match: 1325

first: 1

second: 3

2

hello

(x|y)Match several alternative patterns(re|ba)

red

banter

Rant 

bear

\nreference previous captures where n is the group index starting at 1(b)(\w*)\1

blob

bribe

Bear

bring

\k<name>Reference named captures(?<first>5)(\d*)\k<first>

51245

55

523

51

 

6. Lookahead

Characters can be specified before or after matching, without those characters appearing in the match.

SyntaxDescriptionExample PatternExample MatchesExample non-matches
(?=x)looks ahead at the next characters without using them in the match

an(?=an)

iss(?=ipp)

banana

Mississippi

band

missed

(?!x)looks ahead at next characters to not match onai(?!n)

fail

brail

faint

train

(?<=x)looks at previous characters for a match without using those in the match(?<=tr)a

trail

translate

bear

streak

(?<!x)looks at previous characters to not match on(?!tr)a

bear

translate

trail

strained

 

7. Literal matches and modifiers

Changing the matching rules using modifiers changes how they work.

Syntax

Description

Example PatternExample Matches

Example non- mathes

\Qx\E
Share Article
WhatsappFacebookXLinkedInTelegram
About Author
Akshat Gupta

Founder of Apicle technology private limited

founder of Apicle technology pvt ltd. corporate trainer with expertise in DevOps, AWS, GCP, Azure, and Python. With over 12+ years of experience in the industry. He had the opportunity to work with a wide range of clients, from small startups to large corporations, and have a proven track record of delivering impactful and engaging training sessions.

Are you Confused? Let us assist you.
+1
Explore Data Science Course!
Upon course completion, you'll earn a certification and expertise.
ImageImageImageImage

Popular Courses

Gain Knowledge from top MNC experts and earn globally recognised certificates.
50645 Enrolled
2 Days
From USD 699.00
USD
299.00
Next Schedule March 26, 2025
2362 Enrolled
2 Days
From USD 699.00
USD
279.00
Next Schedule March 24, 2025
25970 Enrolled
2 Days
From USD 999.00
USD
545.00
Next Schedule March 29, 2025
20980 Enrolled
2 Days
From USD 999.00
USD
449.00
Next Schedule March 29, 2025
12659 Enrolled
2 Days
From USD 999.00
USD
545.00
Next Schedule March 29, 2025
PreviousNext

Trending Articles

The most effective project-based immersive learning experience to educate that combines hands-on projects with deep, engaging learning.
WhatsApp