200 likes | 488 Views
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 .
E N D
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 Section 4 Section 5
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”
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.
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.
What Is a “Regular Expression?” So what is it??! A regular expression is just a pattern of text!
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
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.
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
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”.
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
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.
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
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
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
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!
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
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”.
That’s it! • I hope you learned a few things about regular expressions in Ruby! Thanks!