ruby regular expressions n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Ruby Regular Expressions PowerPoint Presentation
Download Presentation
Ruby Regular Expressions

Loading in 2 Seconds...

play fullscreen
1 / 8

Ruby Regular Expressions - PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on

Ruby Regular Expressions. and other languages…. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Ruby Regular Expressions' - jolene-sutton


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
ruby regular expressions

Ruby Regular Expressions

and other languages…

why learn 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
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#UJ

simple example
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

quick exercise
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
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.html

http://rubular.com/

http://coding.smashingmagazine.com/2009/06/01/essential-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
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
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.