and other languages… ruby regular expressions. why learn regular expressions? regex are part of...
TRANSCRIPT
AND OTHER LANGUAGES…
Ruby Regular Expressions
Why Learn Regular Expressions?
RegEx are part of many programmer’s tools vi, grep, PHP, Perl
They provide powerful search (via pattern matching) capabilities
Simple regex are easy, but more advanced patterns can be created as needed
From: http://www.websiterepairguy.com/articles/re/12_re.html
Regular Expressions in Ruby
Objects of type RegexpMatched using =~ operator Constructed as
/pattern/ /pattern/options %r{pattern} %r{pattern}options Regexp.new
Options provide additional info about how pattern match should be done, for example: i – ignore case m – multiline, newline is an ordinary character to match u,e,s,n – specifies encoding, such as UTF-8 (u)
From: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UJ
Simple Example
s = "ruby is cool"s2 = "i love ruby!"if s =~ /Ruby/i
puts "found Ruby - case insensitive"
endif s =~ /[Rr]uby/
puts "found Ruby - Rr option"endif s =~ /\Aruby/
puts "found Ruby at beginning"endif s2 =~ /\Aruby/
puts "found Ruby at beginning"else
puts "ruby is not at the beginning"end
s3 = "I love Java and Ruby and PHP"
if s3 =~ /Java|PHP/puts "what, another language?"
endpartNum = "aZ2"if partNum =~ /[a-z][A-Z][0-9]/
puts "part number is lower-case then upper-case then digit"
endanotherPart = "A45@"if anotherPart =~ /.[0-9]+@/
puts "another part is any character, some digits, @"
end
Play with Regex: rubular.com
Quick Exercise
Create regex for the following. Use rubular.com to check it out.
Phone numbers (303) 555-2222 303.555.2222 3035552222
Date nn-nn-nn Try some other options
Some Resources
http://www.bluebox.net/about/blog/2013/02/using-regular-expressions-in-ruby-part-1-of-3/
http://www.ruby-doc.org/core-2.0.0/Regexp.htmlhttp://rubular.com/http://coding.smashingmagazine.com/2009/06/01/e
ssential-guide-to-regular-expressions-tools-tutorials-and-resources/
http://www.ralfebert.de/archive/ruby/regex_cheat_sheet/
http://stackoverflow.com/questions/577653/difference-between-a-z-and-in-ruby-regular-expressions (thanks, Austin and Santi)
Topic Exploration
http://www.codinghorror.com/blog/2005/02/regex-use-vs-regex-abuse.html
http://programmers.stackexchange.com/questions/113237/when-you-should-not-use-regular-expressions
http://coding.smashingmagazine.com/2009/05/06/introduction-to-advanced-regular-expressions/
http://stackoverflow.com/questions/5413165/ruby-generating-new-regexps-from-strings
A little more motivation to use… http://blog.stevenlevithan.com/archives/10-reasons-to-learn-and-use-
regular-expressions http://www.websiterepairguy.com/articles/re/12_re.html
Submit on BB (3 points) and report back: 3-5 things you want to remember about regex. Include the URL. Feel free to read others not in the list.
In-class Challenge
Review one of the regex cheat sheetsMake up a pattern (e.g., part numbers,
foreign telephone numbers, dates, parse an error log, etc.). Try to use as many aspects of regex as you can. For example, when I pay VISA online, my confirmation includes an embedded date and some other encoding. Create examples of your pattern
Have your neighbor try to recreate your pattern.
Nothing to submit.Is this exercise intended to explore good uses of regex? NO! It’s just a logic puzzle and refresher (or intro) to regex syntax.