[ Try Hack Me ] Regular expressions

Ghi
2 min readMar 23, 2022

Task 1: Introduction

Task 2: Charsets

  • Match all of the following characters: c, o, g

A: [cog]

  • Match all of the following words: cat, fat, hat

A: [cfh]at

  • Match all of the following words: Cat, cat, Hat, hat

A: [cChH]at

  • Match all of the following filenames: File1, File2, file3, file4, file5, File7, file9

A: [fF]ile[1–9]

  • Match all of the filenames of question 4, except “File7” (use the hat symbol)

A: [fF]ile[^7]

Task 3: Wildcards and optional characters

  • Match all of the following words: Cat, fat, hat, rat

A: .at

  • Match all of the following words: Cat, cats

A: [cC]ats?

  • Match the following domain name: cat.xyz

A: cat\.xyz

  • Match all of the following domain names: cat.xyz, cats.xyz, hats.xyz

A: [ch]ats?\.xyz

  • Match every 4-letter string that doesn’t end in any letter from n to z

A: …[^n-z]

  • Match bat, bats, hat, hats, but not rat or rats (use the hat symbol)

A: [^r]ats?

Task 4: Metacharacters and repetitions

  • Match the following word: catssss
  • Match all of the following words (use the * sign): Cat, cats, catsss
  • Match all of the following sentences (use the + sign): regex go br, regex go brrrrrr
  • Match all of the following filenames: ab0001, bb0000, abc1000, cba0110, c0000 (don’t use a metacharacter)
  • Match all of the following filenames: File01, File2, file12, File20, File99
  • Match all of the following folder names: kali tools, kali tools
  • Match all of the following filenames: notes~, stuff@, gtfob#, lmaoo!
  • Match the string in quotes (use the * sign and the \s, \S metacharacters): “2f0h@f0j0%! a)K!F49h!FFOK”
  • Match every 9-character string (with letters, numbers, and symbols) that doesn’t end in a “!” sign
  • Match all of these filenames (use the + symbol): .bash_rc, .unnecessarily_long_filename, and note1

Task 5: Starts with/ ends with, groups, and either/ or

  • Match every string that starts with “Password:” followed by any 10 characters excluding “0”

A: Password:[^0]{10}

  • Match “username: “ in the beginning of a line (note the space!)

A: ^username:\S

  • Match every line that doesn’t start with a digit (use a metacharacter)

A: ^\D

  • Match this string at the end of a line: EOF$

A: EOF\$$

  • Match all of the following sentences:

I use nano

I use vim

A: I use (nano|vim)

  • Match all lines that start with $, followed by any single digit,
    followed by $, followed by one or more non-whitespace characters

A: ^\$\d\$\S+

  • Match every possible IPv4 IP address (use metacharacters and groups)

A: (\d{1,3}\.){3}\d{1,3}

A: (\w+)@(\w+)\.com

Task 6: Conclusion

--

--

Ghi

Passionate cybersecurity learner :3 Ya with me?