Tuesday, July 7, 2009

Regular Expressions: Example based cheatsheet

I need regular expressions all the time because of the programs I use regularly (sed, awk, OpenOffice, etc.). Here's a cheatsheet that my life depends on.

(regular stuff)
/poke/
matches poke, spoke, pokemon.
/or not/ matches or not, poor nothing.

(. matches any character)
/.and./ matches randy, dandy, sandy

(square brackets define character class)
/[aA]rticle/ matches article, Article
/b[aou].k/ matches balk, bonk, bulk
/T[6-9]/ matches T6, T7, T8, T9

(a ^ after [ is a "not" operation)
/T[^6-9] matches T5, T4, Ta, Tt
/[^a-zA-Z] matches 1, #, @

(* matches zero or more occurences of)
/ab*c/ matches ac, abc, abbbbbbc
/s.*tion/ matches station, subtraction, sedition

(longest string between "(" and ")" )
/(.*)/ matches (here) and (there), (()))))))

(shortest string between "(" and ")" )
/([^)]*)/ matches (here) and (there)

(note caret inside [^] means something else)
/^T/ matches T at beginning of line
/T$/ matches T at end of line

(+ = one or more matches of "b")
/ab+c/ matches abbc, abc, but not ac

(? = zero or one match of "b")
/ab?c/ matches abc, ac, but not abbc

(can use parenthesis to group)
/(ab)+c/ matches abc, ababc, but not ac, or abac

(pipe is an or operation)
/(ab)|(ac)/ matches ab or ac.
/(S|A)\. Shanbhag/ matches S. Shanbhag, A. Shanbhag

No comments:

Post a Comment