Techalicious Academy / 2026-01-15-regex-therapy

(Visit our meetup for more great tutorials)

QUICK REFERENCE CARD

Print this out. Tape it to your monitor.

REGEX SYNTAX

Characters:
  .         Any single character
  \d        Digit [0-9]
  \D        Not a digit
  \w        Word char [a-zA-Z0-9_]
  \W        Not a word char
  \s        Whitespace (space, tab, newline)
  \S        Not whitespace

Anchors:
  ^         Start of line
  $         End of line
  \b        Word boundary

Character Classes:
  [abc]     Match a, b, or c
  [^abc]    NOT a, b, or c
  [a-z]     Range: any lowercase letter
  [0-9]     Range: any digit

Quantifiers:
  *         Zero or more
  +         One or more
  ?         Zero or one
  {n}       Exactly n times
  {n,}      n or more times
  {n,m}     Between n and m times
  *?  +?    Non-greedy versions

Groups:
  (...)     Capture group -> $1, $2, etc.
  (?:...)   Non-capturing group
  |         Alternation (OR)

Escaping:
  \.        Literal dot
  \*        Literal asterisk
  \\        Literal backslash

ACK COMMANDS

Basic:
  ack "pattern"           Search current directory
  ack "pattern" dir/      Search specific directory
  ack -i "pattern"        Case insensitive
  ack -w "pattern"        Whole words only

Output Control:
  ack -l "pattern"        List filenames only
  ack -c "pattern"        Count matches per file
  ack -o "pattern"        Print only matched text
  ack -C 3 "pattern"      Show 3 lines of context
  ack -v "pattern"        Invert match (non-matching lines)
  ack -L "pattern"        Files NOT containing pattern

File Filtering:
  ack --perl "pattern"    Search only Perl files
  ack --python "pattern"  Search only Python files
  ack -G "\.txt$" "pat"   Search only files matching regex

RENAME COMMANDS

Basic:
  rename -n 's/old/new/' *       Dry run (ALWAYS DO THIS FIRST)
  rename 's/old/new/' *          Replace first match
  rename 's/old/new/g' *         Replace all matches
  rename 's/old/new/i' *         Case insensitive

Common Tasks:
  rename 's/ /_/g' *             Spaces to underscores
  rename 'y/A-Z/a-z/' *          Lowercase all
  rename 's/\.jpeg$/.jpg/' *     Change extension
  rename 's/^/prefix_/' *        Add prefix
  rename 's/^OLD_//' *           Remove prefix

Capture Groups:
  rename 's/(\d+)_(\w+)/$2_$1/' *   Swap parts
  rename 's/(\d+)/sprintf("%03d",$1)/e' *   Pad numbers

PERL ONE-LINERS

Basic Structure:
  perl -pe 's/old/new/' file    Substitute and print
  perl -ne 'print if /pat/' f   Print matching lines
  perl -i -pe 's/old/new/' f    Edit file in place
  perl -i.bak -pe 's/o/n/' f    Edit with backup

Common Flags:
  -e 'code'    Execute code
  -n           Loop lines, don't auto-print
  -p           Loop lines, auto-print
  -i           Edit files in place
  -i.bak       Edit with backup suffix
  -l           Auto-chomp and newline
  -a           Auto-split into @F
  -F','        Set field separator

Extract Data:
  perl -ne 'print "$1\n" if /(\d+)/' f     Capture numbers
  perl -ane 'print "$F[0]\n"' f            First field
  perl -F',' -ane 'print "$F[1]\n"' f      Second CSV column

Line Filtering:
  perl -ne 'print if /pattern/'            Matching lines
  perl -ne 'print unless /pattern/'        Non-matching
  perl -ne 'print if $. > 1'               Skip header

Common Transformations:
  perl -pe 's/\s+$//'                      Trim trailing space
  perl -pe 's/^\s+//'                      Trim leading space
  perl -pe '$_ = uc'                       Uppercase
  perl -pe '$_ = lc'                       Lowercase

COMMON PATTERNS

Dates:
  \d{4}-\d{2}-\d{2}              YYYY-MM-DD
  \d{1,2}/\d{1,2}/\d{4}          M/D/YYYY or MM/DD/YYYY

Numbers:
  \d+                            Integer
  \d+\.\d+                       Decimal
  -?\d+\.?\d*                    Signed, optional decimal

Filenames:
  \.txt$                         Ends with .txt
  ^IMG_                          Starts with IMG_
  .*\.(?:jpg|png|gif)$           Image extensions

Email-ish:
  \w+@\w+\.\w+                   Simple email pattern

IP Address:
  \d+\.\d+\.\d+\.\d+             IP (not validated)

URLs:
  https?://[^\s]+                Simple URL pattern

TROUBLESHOOTING

Pattern doesn't match?
  - Check case sensitivity (-i flag)
  - Escape special characters (. * + ? etc.)
  - Use word boundaries (\b) for whole words

Wrong part of string matched?
  - Use anchors (^ $)
  - Use non-greedy quantifiers (*? +?)
  - Be more specific in the pattern

rename not working?
  - Did you use -n to test first?
  - Are you using Perl rename (not util-linux)?
  - Check escaping in shell (use single quotes)

ack missing files?
  - Check --ignore-dir settings
  - Use -a for binary files
  - Verify file type with --type-add