Portrait of Michael Limberger

Michael Limberger

Need me? Email mike@limberger.ca

Regex

Quick Reference Card

Tape this near the keyboard

Same facts as the rest of the session, in one place.

Regex glyphs

Glyph Means
. Any single character
\d \D Digit · not a digit
\w \W Word char · not a word char
\s \S Whitespace · not whitespace
^ $ Start of line · end of line
\b Word boundary
[abc] [^abc] a, b, or c · NOT those
* + ? 0+ · 1+ · 0 or 1
{n} {n,} {n,m} Exactly n · n or more · n to m
*? +? Non-greedy versions
(...) (?:...) Capture · non-capturing group
| OR
\. \* \\ Literal dot, star, backslash

ack

Show me.

ack "pattern"
ack "pattern" dir/
ack -i "pattern"
ack -w "pattern"
ack -l "pattern"
ack -c "pattern"
ack -o "pattern"
ack -C 3 "pattern"
ack -v "pattern"
ack -L "pattern"
ack --perl "pattern"
ack --python "pattern"
ack -G "\.txt$" "pat"

-i case-insensitive. -w whole words. -l filenames. -c counts. -o only the match. -C context. -v invert lines. -L files without the pattern. -G filename regex.

rename

rename -n 's/old/new/' *
rename 's/old/new/' *
rename 's/old/new/g' *
rename 's/old/new/i' *
rename 's/ /_/g' *
rename 'y/A-Z/a-z/' *
rename 's/\.jpeg$/.jpg/' *
rename 's/^/prefix_/' *
rename 's/^OLD_//' *
rename 's/(\d+)_(\w+)/$2_$1/' *
rename 's/(\d+)/sprintf("%03d",$1)/e' *

Always -n first. y/// transliterates. /e evaluates Perl in the replacement.

Perl one-liners

perl -pe 's/old/new/' file
perl -ne 'print if /pat/' f
perl -i -pe 's/old/new/' f
perl -i.bak -pe 's/o/n/' f
perl -ne 'print "$1\n" if /(\d+)/' f
perl -ane 'print "$F[0]\n"' f
perl -F',' -ane 'print "$F[1]\n"' f
perl -ne 'print unless /pattern/'
perl -ne 'print if $. > 1'
perl -pe 's/\s+$//'
perl -pe 's/^\s+//'
perl -pe '$_ = uc'
perl -pe '$_ = lc'

Flags: -e code, -n loop no print, -p loop and print, -i in place, -l newlines, -a split into @F, -F',' field separator. $_ is the current line.

Patterns you will reuse

\d{4}-\d{2}-\d{2}              YYYY-MM-DD
\d{1,2}/\d{1,2}/\d{4}          M/D/YYYY
\d+                            Integer
\d+\.\d+                       Decimal
-?\d+\.?\d*                    Signed, optional decimal
\.txt$                         Ends with .txt
^IMG_                          Starts with IMG_
.*\.(?:jpg|png|gif)$           Image extensions
\w+@\w+\.\w+                   Simple email
\d+\.\d+\.\d+\.\d+             IP (not validated)
https?://[^\s]+                Simple URL

When it breaks

Pattern does not match? Check case sensitivity (-i). Escape special characters. Use word boundaries (\b) for whole words.

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

rename not working? Did you use -n first? Are you using Perl rename, not util-linux? Check escaping in the shell. Use single quotes.

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