Testing your regular expressions can be done in two ways:
- Create a text file with some test paragraphs on a Unix machine, then use
egrep <pattern> <file>
to see what matches and what doesn't. - Use an online editor like regexr.com. Add your text in the "Text" field and type your expressions in the "Expression" field. This method is highly recommended for ease of use.
When searching for specific strings or patterns in a file or block of text, regular expressions (regex) come in handy.
[abc]
: Matches any occurrence of 'a', 'b', or 'c'.[abc]zz
: Matches 'azz', 'bzz', and 'czz'.
[a-c]zz
: Same as above.[a-cx-z]zz
: Matches 'azz', 'bzz', 'czz', 'xzz', 'yzz', and 'zzz'.[a-zA-Z]
: Matches any single letter (lowercase or uppercase).file[1-3]
: Matches 'file1', 'file2', and 'file3'.
[^k]ing
: Matches 'ring', 'sing', '$ing', but not 'king'.[^a-c]at
: Matches 'fat' and 'hat', but not 'bat' or 'cat'.
- Charsets match any occurrence of the specified characters, not just strings.
- Type characters in the same order as the questions to avoid confusion.
- Efficient regex means:
- Be specific: Use
[a-c]
instead of[a-z]
if only matching 'a' to 'c'. - Don't be too specific: Use
[a-z]
for a broader range to avoid complexity.
- Be specific: Use
-
Match characters
c
,o
,g
:[cog]
-
Match words
cat
,fat
,hat
:[cfh]at
-
Match words
Cat
,cat
,Hat
,hat
:[CcHh]at
-
Match filenames
File1
,File2
,file3
,file4
,file5
,File7
,file9
:[Ff]ile[1-9]
-
Match all filenames except "File7":
[Ff]ile[^7]
.
: Matches any single character except the line break.?
: Makes the preceding character optional.
-
Match words
Cat
,fat
,hat
,rat
:.at
-
Match words
Cat
,cats
:[Cc]ats?
-
Match domain name
cat.xyz
:cat\.xyz
-
Match domain names
cat.xyz
,cats.xyz
,hats.xyz
:[ch]ats?\.xyz
-
Match 4-letter strings not ending with letters
n
toz
:...[^n-z]
-
Match
bat
,bats
,hat
,hats
, but notrat
orrats
(use the hat symbol)[^r]ats?
\d
: Matches a digit.\D
: Matches a non-digit.\w
: Matches an alphanumeric character.\W
: Matches a non-alphanumeric character.\s
: Matches a whitespace character.\S
: Matches everything else.
{n}
: Matches exactlyn
times.{n,m}
: Matches betweenn
andm
times.{n,}
: Matchesn
or more times.*
: Matches 0 or more times.+
: Matches 1 or more times.
-
Match word
catssss
:cats{4}
-
Match words
Cat
,cats
,catsss
:[Cc]ats*
-
Match sentences
regex go br
,regex go brrrrrr
:regex go br+
-
Match filenames
ab0001
,bb0000
,abc1000
,cba0110
,c0000
:[abc]{1,3}[01]{4}
-
Match filenames
File01
,File2
,file12
,File20
,File99
:[Ff]ile\d{1,2}
-
Match all of the following folder names:
kali tools
,kali tools
:kali\s+tools
-
Match all of the following filenames:
notes~
,stuff@
,gtfob#
,lmaoo!
:\w{5}\W
-
Match the string in quotes (use the
*
sign and the\s
,\S
metacharacters):"2f0h@f0j0%! a)K!F49h!FFOK"
:\S*\s*\S*
-
Match every 9-character string (with letters, numbers, and symbols) that doesn't end in a "!" sign:
\S{8}[^!]
-
Match all of these filenames (use the
+
symbol):.bash_rc
,.unnecessarily_long_filename
, andnote1
:\.?[\w_]+
^
: Matches the start of a line.$
: Matches the end of a line.
(pattern)
: Defines a group.|
: Specifies an either/or pattern.
-
Match string starting with "Password:" followed by 10 characters excluding "0":
Password:[^0]{10}
-
Match "username: " at the beginning of a line:
^username:\s
-
Match lines not starting with a digit:
^\D
-
Match string
EOF$
at the end of a line:EOF\$$
-
Match sentences:
I use vim
I use nano
I use (nano|vim)
-
Match lines starting with
$
followed by a digit, another$
, and non-whitespace characters:\$\d\$\S+
-
Match any IPv4 address:
(\d{1,3}\.){3}\d{1,3}
-
Match emails
hello@tryhackme.com
,username@domain.com
,dummy_email@xyz.com
and group the username and domain:(\w+)@(\w+)\.com
This cheatsheet is created by Purva Patel.