and other languages… ruby regular expressions. why learn regular expressions? regex are part of...

8
AND OTHER LANGUAGES… Ruby Regular Expressions

Upload: margaretmargaret-cameron

Post on 28-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

AND OTHER LANGUAGES…

Ruby Regular Expressions

Page 2: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

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

Page 3: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

Regular Expressions in Ruby

Objects of type RegexpMatched 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

Page 4: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

Simple Example

s = "ruby is cool"s2 = "i love ruby!"if s =~ /Ruby/i

puts "found Ruby - case insensitive"

endif s =~ /[Rr]uby/

puts "found Ruby - Rr option"endif s =~ /\Aruby/

puts "found Ruby at beginning"endif 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?"

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

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

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

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

end

Play with Regex: rubular.com

Page 5: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

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

Page 7: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

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.

Page 8: AND OTHER LANGUAGES… Ruby Regular Expressions. Why Learn Regular Expressions? RegEx are part of many programmer’s tools  vi, grep, PHP, Perl They provide

In-class Challenge

Review one of the regex cheat sheetsMake 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.