Regex lookaround cheatsheet


I have a fairly terrible memory but I’ve found writing and referring back to cheatsheets to be a pretty effective way to memorize things.

I ended up referring back to my previous cheatsheet on git reset quite a few times soon after I wrote it, so when I came across this great post on regex features in ruby I thought I’d share the lookarounds section because I always forget those too!

SyntaxDescriptionExampleResult
(?=X)Positive lookahead"Ruby"[/.(?=b)/]"u"
(?!X)Negative lookahead"Ruby"[/.(?!u)/]"u"
(?<=X)Positive lookbehind"Ruby"[/(?<=u)./]"b"
(?<!X)Negative lookbehind"Ruby"[/(?<!R|^)./]"b"

Checkout the original blog post for the rest of the regex tips or the Idiosyncratic Ruby site for more ruby tips!