RegEx

Regular expressions (regex) are powerful for searching and analyzing code


Basic Syntax

  • . : Matches any character except newline.

  • ^ : Matches start of a line.

  • $ : Matches end of a line.

  • * : Matches 0 or more repetitions.

  • + : Matches 1 or more repetitions.

  • ? : Matches 0 or 1 repetition (optional).

  • {n} : Matches exactly n repetitions.

  • {n,} : Matches n or more repetitions.

  • {n,m} : Matches between n and m repetitions.


Character Classes

  • [abc] : Matches any of a, b, or c.

  • [^abc] : Matches any character except a, b, or c.

  • [a-z] : Matches any lowercase letter.

  • [A-Z] : Matches any uppercase letter.

  • [0-9] : Matches any digit.

  • \d : Matches any digit (same as [0-9]).

  • \D : Matches any non-digit.

  • \w : Matches any word character (letters, digits, underscore).

  • \W : Matches any non-word character.

  • \s : Matches any whitespace.

  • \S : Matches any non-whitespace.


Grouping and Capturing

  • (abc) : Capturing group for abc.

  • (?:abc) : Non-capturing group.

  • (?P<name>abc) : Named capturing group.


Alternation

  • a|b : Matches a or b.


Anchors

  •  : Word boundary.

  • \B : Non-word boundary.


Lookarounds

  • (?=abc) : Positive lookahead (abc must follow).

  • (?!abc) : Negative lookahead (abc must NOT follow).

  • (?<=abc) : Positive lookbehind (abc must precede).

  • (?<!abc) : Negative lookbehind (abc must NOT precede).


Escaping Special Characters

  • Use \ before special characters: e.g., \. matches a literal dot.


Common Practical Patterns

  • Match function calls: \w+\(

  • Match quoted strings: "[^"]*" or '[^']*'

  • Match parameters in parentheses: \([^)]*\)

  • Match URLs: https?:\/\/[\w\.-]+


Useful Grep Examples for OSWE

  • Find PHP superglobals:

  • Find Java parameter access:

  • Find Python request data:


Tips for OSWE

  • Combine regex with grep -E for extended patterns.

  • Use -RIn for recursive search with line numbers.

  • Test regex with tools like regex101.comarrow-up-right.

Last updated