Unlock Growth - Lock Savings
Offer Ends Soon

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
5664
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:

Syntax Description Example Pattern Example Matches Example non- matches
^, \A Matches the start of line ^r, \Ar

run

recess 

dog

cat

$, \Z Match the end of line n$, n\Z

corn

fun

mobile

speaker

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

the rat ran

the rat ate

ratskin 

ratflow

\B Match 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.

Syntax Description Example Pattern Example Matches Example non- matches
. Anything except for a line break c.e

cheap

clean

 

Acert

cent

\d match a digit \d

6060-896

2b|^2b

Tw 

**___

\D Match a non-digit \D The 5 cats ate 12 angry rats

52

10032

\w

 

Match word characters \wee\w

Trees

bees

The bee

Eels eat skin

\W Match non-word characters \Wbat\W

At bat

Swing the bat slow

Wombat

bat53

\s Match whitespace \sfox\s

The fox ate

The fox ran

It’s the fox

fox-fur

\S Match non-whitespace \See\S

Trees

beef

The bee stung

The tall tree

\metacharacter Escape 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

Syntax Description Example Pattern Example Matches Example non-matches
[xy] Match several characters gr[ea]y

Gray

grey

Green

greek

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

Amber

brand

Fox 

join

[^xy] Does not match several characters gr[^ea]y

Green

greek

Gray

grey

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

4^3

4.2

44

23

 

4.  Repetition

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

Syntax Description Example Pattern Example Matches Example non- matches
x* Matches zero or more times ar*o

Cacao

carrot

Arugula

artichoke

x+ Matches one or more times re+

Green

tree

Trap

ruined

x? Matches zero or one times ro?a

roast

rant

Root

rear

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

deer

seer

Red

enter

x{m,} Match m or more times 2{3,}4

671-2224

22224

224

123

x{m,n} Match between m and n times 12{1,3}3

1234

1222389

15335

122223

x*?,x+?, etc match the minimum number of times - known as a lazy quantifier re+?

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.

Syntax Description Example Pattern Example Matches Example 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

\n reference 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.

Syntax Description Example Pattern Example Matches Example 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 on ai(?!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 Pattern Example 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:

Syntax Description Example Pattern Example Matches Example non- matches
^, \A Matches the start of line ^r, \Ar

run

recess 

dog

cat

$, \Z Match the end of line n$, n\Z

corn

fun

mobile

speaker

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

the rat ran

the rat ate

ratskin 

ratflow

\B Match 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.

Syntax Description Example Pattern Example Matches Example non- matches
. Anything except for a line break c.e

cheap

clean

 

Acert

cent

\d match a digit \d

6060-896

2b|^2b

Tw 

**___

\D Match a non-digit \D The 5 cats ate 12 angry rats

52

10032

\w

 

Match word characters \wee\w

Trees

bees

The bee

Eels eat skin

\W Match non-word characters \Wbat\W

At bat

Swing the bat slow

Wombat

bat53

\s Match whitespace \sfox\s

The fox ate

The fox ran

It’s the fox

fox-fur

\S Match non-whitespace \See\S

Trees

beef

The bee stung

The tall tree

\metacharacter Escape 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

Syntax Description Example Pattern Example Matches Example non-matches
[xy] Match several characters gr[ea]y

Gray

grey

Green

greek

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

Amber

brand

Fox 

join

[^xy] Does not match several characters gr[^ea]y

Green

greek

Gray

grey

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

4^3

4.2

44

23

 

4.  Repetition

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

Syntax Description Example Pattern Example Matches Example non- matches
x* Matches zero or more times ar*o

Cacao

carrot

Arugula

artichoke

x+ Matches one or more times re+

Green

tree

Trap

ruined

x? Matches zero or one times ro?a

roast

rant

Root

rear

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

deer

seer

Red

enter

x{m,} Match m or more times 2{3,}4

671-2224

22224

224

123

x{m,n} Match between m and n times 12{1,3}3

1234

1222389

15335

122223

x*?,x+?, etc match the minimum number of times - known as a lazy quantifier re+?

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.

Syntax Description Example Pattern Example Matches Example 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

\n reference 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.

Syntax Description Example Pattern Example Matches Example 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 on ai(?!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 Pattern Example Matches

Example non- mathes

\Qx\E
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.
Image Image Image Image

Trending Articles

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