Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Cameron Modified over 9 years ago
1
AND OTHER LANGUAGES… Ruby Regular Expressions
2
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.htmlhttp://www.websiterepairguy.com/articles/re/12_re.html
3
Regular Expressions in Ruby Objects of type Regexp Matched 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#UJhttp://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UJ
4
Simple Example s = "ruby is cool" s2 = "i love ruby!" if s =~ /Ruby/i puts "found Ruby - case insensitive" end if s =~ /[Rr]uby/ puts "found Ruby - Rr option" end if s =~ /\Aruby/ puts "found Ruby at beginning" end if 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?" end partNum = "aZ2" if partNum =~ /[a-z][A-Z][0-9]/ puts "part number is lower-case then upper-case then digit" end anotherPart = "A45@" if anotherPart =~ /.[0-9]+@/ puts "another part is any character, some digits, @" end Play with Regex: rubular.com
5
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
6
Some Resources http://www.bluebox.net/about/blog/2013/02/using- regular-expressions-in-ruby-part-1-of-3/ 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.html http://rubular.com/ http://coding.smashingmagazine.com/2009/06/01/essent ial-guide-to-regular-expressions-tools-tutorials-and- resources/ http://coding.smashingmagazine.com/2009/06/01/essent ial-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) http://stackoverflow.com/questions/577653/difference- between-a-z-and-in-ruby-regular-expressions
7
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://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://coding.smashingmagazine.com/2009/05/06/introduction-to- advanced-regular-expressions/ http://stackoverflow.com/questions/5413165/ruby-generating-new-regexps- from-strings 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://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.
8
In-class Challenge Review one of the regex cheat sheets Make 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.