Skip to content

Instantly share code, notes, and snippets.

@kris-luminar
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kris-luminar/be20aef88e91e7af5816 to your computer and use it in GitHub Desktop.
Save kris-luminar/be20aef88e91e7af5816 to your computer and use it in GitHub Desktop.
Iowa Ruby Brigade Regex 1/20/2015

The ugliest Regex Ever

http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

Word character

\w

Non-Word character

\W

Whitespace

\s

matching against a url with or without 'www.'

http://rubular.com/r/YS5dAgYaDW

matching against phone numbers

http://rubular.com/r/NdUkySPpdg

%r

stub_request(:get, /https:\/\/mbc-test-rates\.herokuapp\.com\/calculate-premium\?action=premium_and_commission&controller=contract_bonds&t=.*/)
stub_request(:get, %r|https://mbc-test-rates\.herokuapp\.com/calculate-premium\?action=premium_and_commission&controller=contract_bonds&t=.*|)

removing hashrockets in vim

to_return(:status => 404, :body => body, :headers => {})
%s;\(\w*\) => ;\1 ;g

=~ and !~

"That rug really tied the room together." =~ /room/
=> character position of the first matching character
"That rug really tied the ".length

/room/ !~ "That rug really tied the room together."
=> false

Matchdata

string = "We believe in nothing, Lebowski!"
m = /(.*)believe(.*)/.match(string)
m.captures
=> an array with all your captures

m[0] OR m.to_s
=> full string that matches

m[1]
=> first match

m[2]
=> next match

http://rubular.com/r/Kaj8mPEA4c

Capitals

/believe/ =~ "We Believe in nothing, Lebowski!"
=> nil

/believe/i =~ "We Believe in nothing, Lebowski!"
=> nil

http://rubular.com/r/yZytLTRb4l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment