1 / 19

Ruby: Regular Expressions

Ruby: Regular Expressions. Taylor Klotz Programming Languages – Spring 2011. Lesson Order. Section 1. Section 2. Section 3. What is a “Regular Expression?” Formal Definition. How Does Ruby Use Regular Expressions?. Ruby Code Example # 2. Ruby Code Example #3. Ruby Code Example #1 .

jewell
Download Presentation

Ruby: Regular Expressions

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby: Regular Expressions Taylor Klotz Programming Languages – Spring 2011

  2. Lesson Order Section 1 Section 2 Section 3 What is a “Regular Expression?” Formal Definition How Does Ruby Use Regular Expressions? Ruby Code Example #2 Ruby Code Example #3 Ruby Code Example #1 Section 4 Section 5

  3. What Is a “Regular Expression?” • A “regular expression,” in computing, provides an efficient way to quickly search and match strings of text. • Regular expressions are written in a finite syntax that can be analyzed by their expression or lexical analyzer. • These expression describe a set of strings. A sequence of characters, like “bar” can be used to identify larger phrases containing them: “Prybar” “barbecue” “barber” “Downtown bar”

  4. What Is a “Regular Expression?” A Brief Bit of History • Regular Expressions come from a combination of automata theory and formal language theory. • Mathematician Stephen Kleenewas the first to experiment with regular expressions. • His work eventually led the Unix/Linux tool grep’s incorporation of regular expressions to search files.

  5. What Is a “Regular Expression?” Here’s some lofty stuff.. • “Regular Expressions consist of constants and operators that denote sets of strings and operations over these sets.” -Wikipedia • Definitions of regular expressions are designed with parsing in mind. • Regular expressions describe and ultimately express regular languages.

  6. What Is a “Regular Expression?” So what is it??! A regular expression is just a pattern of text!

  7. Lesson Order Section 1 Section 2 Section 3 What is a “Regular Expression?” Formal Definition How Does Ruby Use Regular Expressions? Ruby Code Example #2 Ruby Code Example #3 Ruby Code Example #1 Section 4 Section 5

  8. Regular Expressions in Ruby • Ruby supports regular expressions as a feature of its language. • Other languages like C, Java and Python only offer use of regular expressions through additional libraries. • This makes the process of incorporating and wielding regular expressions in our Ruby code quick and painless.

  9. Regular Expressions in Ruby Ruby’s Regex Syntax (Oh! “Regex” is a synonym of Regular Expression) Ruby syntax EXPLANATION • [ ] • \w • \W • \S • \d • \D • + • | • ( ) • Range, [a-d] means all letters a through z inclusive. • Word character • Non-word character • Space character • Digit character, [0-9] • Non-digit character • one or more repetitions of the preceding • Either preceding expressions may match • Grouping

  10. Regular Expressions in Ruby • In Ruby, regular expressions are objects of type “Regexp.” • The easiest way to match a pattern (regular expression) and a string is with the match methodwith the match operator “=~”. • var1 = /Ruby/.match(“Elephants wear white hats in bars”) • puts var1.class • var2 = “bars” =~ /Ruby/ • puts var2 • This will return the starting index of “bars”.

  11. Lesson Order Section 1 Section 2 Section 3 What is a “Regular Expression?” Formal Definition How Does Ruby Use Regular Expressions? Ruby Code Example #2 Ruby Code Example #3 Ruby Code Example #1 Section 4 Section 5

  12. Ruby Code Example #1 • In this example, we’ll learn: • More about how to implement the match method. • How to “capture” segments of matching strings. • How to use the output from the match method.

  13. Ruby Code Example #1 • string = "My home phone number is (706)565-4883." • telephone = /\((\d{3})\)\s+(\d{3})-(\d{4})/   • mmaker = telephone.match(string)   • unlessmmaker • puts“No Match!" •   exit   • end • print“Initial String: "   • putsmmaker.string • print“Complete matching section: "   • putsmmaker[0]   • puts "The 3 subsections: "   • 3.timesdo |index|   • puts“Section ##{index + 1}: #{mmaker.captures[index]}"   • end

  14. Ruby Code Example #1 • The output looks like this: • Initial String: My home phone number is (706) 565-4883. • Complete matching section: (706) 565-4883 • Section #1: 706 • Section #2: 565 • Section #3: 4883

  15. Lesson Order Section 1 Section 2 Section 3 What is a “Regular Expression?” Formal Definition How Does Ruby Use Regular Expressions? Ruby Code Example #2 Ruby Code Example #3 Ruby Code Example #1 Section 4 Section 5

  16. Ruby Code Example #2 • In this example, we’ll learn: • Really simple text substitution/replacement with Ruby. • string = “I love ruby, ruby is so much fun!" • String.gsub!(“ruby”, “Ruby Programming Language”) • Puts “#{string}” • This short piece of code spits out the following: • I love Ruby Programming Language, Ruby Programming Language is so much fun!

  17. Lesson Order Section 1 Section 2 Section 3 What is a “Regular Expression?” Formal Definition How Does Ruby Use Regular Expressions? Ruby Code Example #2 Ruby Code Example #3 Ruby Code Example #1 Section 4 Section 5

  18. Ruby Code Example #3 • We’ll end this tutorial with a very simple regular expression search. Easily implemented into existing programs. • string1 = “Dogs rule!" • string2 = “Cats drool!” • if (string1 =~ /Cats(.*)/) • puts “String1 starts with “Cats”.” • end • if (string2 =~ /Cats(.*)/) • puts “String2 starts with “Cats”.” • Which outputs: String2 starts with “Cats”.

  19. That’s it! • I hope you learned a few things about regular expressions in Ruby! Thanks!

More Related