Tool of Choice: Regex101 OpSec Note: Never paste sensitive client data (PII, passwords) into online regex testers. Use them for learning logic, but run the final regex locally in your terminal.

1. The Building Blocks (Cheatsheet)

Charsets: []

Think of square brackets as a single “slot” that can accept multiple candidates.

  • Logic: “I need one character here, but it can be ‘a’, ‘b’, or ‘c’.”
  • [abc] -> Matches a, b, or c.
  • [a-z] -> Matches any lowercase letter.
  • [^abc] -> Matches anything EXCEPT a, b, or c.

Wildcards & Escaping

  • . (Dot) -> The Joker. Matches any single character (except a newline).
  • \. (Escaped Dot) -> Matches a literal dot character (e.g., in 10.10.10.10).

Metacharacters (The Shortcuts)

Why type [0-9] when you can type \d?

  • \d -> Any digit (0-9).
  • \w -> Any “word” character (Alphanumeric + underscore).
  • \s -> Whitespace (Space, Tab, Newline).
  • Capitalized: The opposite. \D is “Not a digit”, \S is “Not whitespace”.

Repetition (The Multipliers)

These apply to the character immediately preceding them.

  • ? -> 0 or 1 (Optional). “Maybe it’s there, maybe not.”
  • * -> 0 or more. “It might not be there, or there might be a million of them.”
  • + -> 1 or more. “It MUST be there at least once.”
  • {n} -> Exactly n times.
  • {min,max} -> Between min and max times.

2. The “Boss Battles” (Complex Challenges)

You mentioned the last two were hard. Based on standard Regex rooms, these are almost always IP Addresses and Emails. Here is the logic for solving them.

Challenge A: Matching an IP Address

The Goal: Match something like 192.168.1.1 but not 999.999.999.999 (simplified).

The Logic: An IP is: Number Dot Number Dot Number Dot Number.

  1. The Number: \d{1,3} (One to three digits).
  2. The Separator: \. (We must escape the dot, otherwise . matches any char).
  3. The Anchor: We often use ^ and $ to ensure we match the whole line, not just a part of it.

The Regex:

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
  • \b represents a “Word Boundary” (start/end of the word), ensuring we don’t match the middle of a longer string.

Challenge B: Matching an Email Address

The Goal: Match username@domain.com.

The Logic:

  1. Username: Can contain letters, numbers, dots, underscores.
  • [a-zA-Z0-9._-]+
  1. The @ Symbol: Literal @.
  2. Domain Name: Letters, numbers, dashes.
  • [a-zA-Z0-9.-]+
  1. The Dot: Literal \.
  2. The TLD (Top Level Domain): Like .com, .org. Usually 2 letters or more.
  • [a-zA-Z]{2,}

The Regex:

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Challenge C: The “Either/Or” Grouping

The Goal: Match “I like cats” or “I like dogs”.

The Logic: We use parentheses () to group the options and the pipe | to say “OR”.

The Regex:

I like (cats|dogs)

3. Practical Usage (Where you use this IRL)

Grep (Searching Files)

When you are looking for a password in a massive log file:

# Search for lines that look like "Password = [something]"
grep -E "Password\s?=\s?\w+" debug.log

Input Validation (Web Apps)

When checking if a user submitted a valid phone number format:

^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

(Matches (555) 555-5555 or 555-555-5555)

Conclusion

Regex is a language you usually “write once, read never.” Don’t memorize every single rule. Just understand Character Classes ([]), Quantifiers (+, *), and Anchors (^, $). For everything else, there’s Google and Regex101.