ruby regular expressions n.
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

  • 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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
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


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
  • 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)


simple example
Simple Example

s = "ruby is cool"

s2 = "i love ruby!"

if s =~ /Ruby/i

puts "found Ruby - case insensitive"


if s =~ /[Rr]uby/

puts "found Ruby - Rr option"


if s =~ /\Aruby/

puts "found Ruby at beginning"


if s2 =~ /\Aruby/

puts "found Ruby at beginning"


puts "ruby is not at the beginning"


s3 = "I love Java and Ruby and PHP"

if s3 =~ /Java|PHP/

puts "what, another language?"


partNum = "aZ2"

if partNum =~ /[a-z][A-Z][0-9]/

puts "part number is lower-case then upper-case then digit"


anotherPart = "A45@"

if anotherPart =~ /.[0-9]+@/

puts "another part is any character, some digits, @"


Play with Regex:

quick exercise
Quick Exercise
  • Create regex for the following. Use 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, Austin and Santi)

topic exploration
Topic Exploration

A little more motivation to use…

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.