syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Ruby how do I match a regular expression and a string?

When I write Ruby code, I often encounter a use-case where I want to match a regular expression and a string.

Ruby offers several ways to do this.

Way 1 (hard-coded regular expression):
# Simple demo of looking for smaller string in larger string.
mypath = "/app/views/posts/_index.haml"
if mypath =~ /index.haml/
  p 'mypath contains index.haml'
else
  p 'mypath does not contain index.haml'
end
Way 2 (create regular expression from string variable):
# Simple demo of looking for smaller string in larger string.
mypath      = "/app/views/posts/_index.haml"
smallstring = "index.haml"
if mypath =~ Regexp.new(smallstring)
  p 'mypath contains index.haml'
else
  p 'mypath does not contain index.haml'
end
Way 3 (use match() instead of =~):
# Simple demo of looking for smaller string in larger string.
mypath      = "/app/views/posts/_index.haml"
smallstring = "index.haml"
if mypath.match(Regexp.new(smallstring))
  p 'mypath contains index.haml'
else
  p 'mypath does not contain index.haml'
end
Way 4 (use curly braces instead of slashes or Regexp.new()):
# Simple demo of looking for smaller string in larger string.
mypath      = "/app/views/posts/_index.haml"
# if mypath =~ %r{index.haml} # This works too
if mypath.match(%r{index.haml})
  p 'mypath contains index.haml'
else
  p 'mypath does not contain index.haml'
end
Way 5 (remember what I matched):
# Simple demo: remember what I matched.
mypath    = "/app/views/posts/_index.haml"
if mypath =~ %r{(/app)(/views)(/posts)(/_index.haml)}
  p 'mypath contains 4 matches'
else
  p 'mypath does not contain 4 matches'
end

# My matches are stored in a MatchData object.
# Name of object is $~
p $~

# MatchData object remembers LHS of =~
p $~[0]

# MatchData object remembers 1st match
p $~[1]

# MatchData object remembers 2nd match
p $~[2]

# MatchData object remembers 3rd match
p $~[3]

# MatchData object remembers 4th match
p $~[4]

# Plain $ sign easier to type:
p $1 # same as $~[1]
p $2
p $3
p $4

# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ ruby /tmp/jj.rb
# "mypath contains 4 matches"
# #<MatchData "/app/views/posts/_index.haml" 1:"/app" 2:"/views" 3:"/posts" 4:"/_index.haml">
# "/app/views/posts/_index.haml"
# "/app"
# "/views"
# "/posts"
# "/_index.haml"
# "/app"
# "/views"
# "/posts"
# "/_index.haml"
# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ 
Way 6 (match and replace a string):
# Simple demo: match and replace a string in large string:
large_string = 'Hello , intelligent , but inferior, creatures of Earth.'
smallstring = large_string.gsub(/,/,'').gsub(/ /,'').sub(/intel.*/, ' World')
p smallstring

# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ ruby /tmp/jj.rb
# "Hello World"
# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ 
Way 7 (3 more $ vars):
# Simple demo: 3 dollar variables

alien_string = 'Hello intelligent Earthlings. Talk to me.'

alien_string =~ /intelligent/

p 'text before the match:'
p $`

p 'the match:'
p $&

p 'text after the match:'
p $'

# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ ruby /tmp/jj.rb
# "text before the match:"
# "Hello "
# "the match:"
# "intelligent"
# "text after the match:"
# " Earthlings. Talk to me."
# dan@hp ~/ruby-code $ 
# dan@hp ~/ruby-code $ 

syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me