Skip to content

Instantly share code, notes, and snippets.

@janko
Created May 27, 2015 23:05
Show Gist options
  • Save janko/a2859704fb557827fb16 to your computer and use it in GitHub Desktop.
Save janko/a2859704fb557827fb16 to your computer and use it in GitHub Desktop.
Advanced Regex features in Ruby (my presentation from our local Ruby meetups)
############
# GROUPING #
############
def Given(*) end
Given(/There (is|are) some links?/) { } # ERROR
Given(/There (?:is|are) some links?/) { }
##############
# REPETITION #
##############
/<.+>/.match "<a><b>" # => #<MatchData "<a><b>">
/<.+?>/.match "<a><b>" # => #<MatchData "<a>">
###########
# ANCHORS #
###########
/^[\w ]+$/ === "John Smith\nUPDATE users SET admin = true" # => true
/\A[\w ]+\Z/ === "John Smith\nUPDATE users SET admin = true" # => false
# positive lookahead (?=) & lookbehind (?<=)
"ScheduledEvent"
.split(/(?=[A-Z])/) # => ["Scheduled", "Event"]
.map(&:downcase).join("_") # => "scheduled_event"
# negative lookahead (?!) & lookbehind (?<!)
(<<-EOS).scan(/[a-zA-Z0-9 ]+? (?!LAME)/) # => ["Film ", "Film ", "2 "]
Film 1
Film 2 LAME
EOS
###########
# OPTIONS #
###########
/pattern/i # ignore case
/pattern/m # extend "." to match newline
/pattern/x # free-spacing mode
/pattern/o # perform #{} interpolation only once
/pattern/mi
/a(?i:b)c/ === "aBc" # => true
/a(?-i:b)c/i === "ABC" # => false
/(?i-mx:a)/
###########
# UNICODE #
###########
("a".."z").to_a # => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
("┌".."╋").to_a # => ["┌", "┍", "┎", "┏", "┐", "┑", "┒", "┓", "└", "┕", "┖", "┗", "┘", "┙", "┚", "┛", "├", "┝", "┞", "┟", "┠", "┡", "┢", "┣", "┤", "┥", "┦", "┧", "┨", "┩", "┪", "┫", "┬", "┭", "┮", "┯", "┰", "┱", "┲", "┳", "┴", "┵", "┶", "┷", "┸", "┹", "┺", "┻", "┼", "┽", "┾", "┿", "╀", "╁", "╂", "╃", "╄", "╅", "╆", "╇", "╈", "╉", "╊", "╋"]
/[┌-╋]/ === "┣" # => true
/[\u{250C}-\u{254B}]/ === "┣"
#####################
# CHARACTER CLASSES #
#####################
/^[a-zA-Z]+$/ === "Ryan" # => true
/^[a-zA-Z]+$/ === "Krleža" # => false
/^\w+$/ === "Krleža" # => false
/^[[:alpha:]8]+$/ === "8Krleža" # => true
/^[[:alpha:]]+$/ === "象形字" # => true
/^[[:upper:]]+$/
/^[[:lower:]]+$/
/^[^[:punct:]]+$/ === ".,;'-" # => false
/^\p{Alpha}+$/
/^\p{ASCII}+$/
/^\p{Arabic}+$/ === "غظضخثت" # => true
/^\p{Sc}+$/ === "$£" # => true
/^\p{^P}+$/
Regexp.union("foo", /bar/, /baz/i) # => /foo|(?-mix:bar)|(?i-mx:baz)/
`ri Regexp`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment