Reg EX
Upcoming SlideShare
Loading in...5
×
 

Reg EX

on

  • 676 views

 

Statistics

Views

Total Views
676
Views on SlideShare
676
Embed Views
0

Actions

Likes
0
Downloads
21
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Reg EX Reg EX Presentation Transcript

  • REGULAR EXPRESSIONS /regex will find me/Friday, March 4, 2011
  • WHAT ARE REGULAR EXPRESSIONS • Regular expressions allow matching and manipulation of textual data. • Abbreviated as regex or regexp, or alternatively, just patternsFriday, March 4, 2011
  • REGULAR EXPRESSIONS IN RUBYFriday, March 4, 2011
  • USING REGULAR EXPRESSIONS • Scan a string for multiple occurrences of a pattern. • Replace part of a string with another string. • Split a string based on a matching separator.Friday, March 4, 2011
  • REGULAR EXPRESSION SYNTAX • Regular expressions are put between two forward slashes (/match_me/) • They are escaped with a backward slash ().Friday, March 4, 2011
  • CHARACTERS THAT NEED TO BE ESCAPED . | ( ) [ ] { } + ^ $ * ?Friday, March 4, 2011
  • REGEX BASICS • [abc] A single character: a, b or c • [^abc] Any single character but a, b, or c • [a-z] Any single character in the range a-z • [a-zA-Z] Any single character in the range a-z or A-Z • ^ Start of line • $ End of line • A Start of string • z End of stringFriday, March 4, 2011
  • REGEX BASICS CONT... • . Any single character • s Any whitespace character • S Any non-whitespace character • d Any digit • D Any non-digit • w Any word character (letter, number, underscore) • W Any non-word character • b Any word boundary characterFriday, March 4, 2011
  • REGEX BASICS CONT... • (...) Capture everything enclosed • (a|b) a or b • a? Zero or one of a • a* Zero or more of a • a+ One or more of a • a{3} Exactly 3 of a • a{3,} 3 or more of a • a{3,6} Between 3 and 6 of aFriday, March 4, 2011
  • REGEX: .MATCH >> category = "power tools" => "power tools" >> puts "on Sale" if category.match(/power tools/) on Sale >> puts "on Sale" if /power tools/.match(category) on SaleFriday, March 4, 2011
  • REGEX: =~ >> category = "shoes" => "shoes" >> puts "15 % off" if category =~ /shoes/ 15 % off >> puts "15 % off" if /shoes/ =~ category 15 % off >> /pants/ =~ category => nil >> /shoes/ =~ category => 0 >> category = "womens shoes” >> /shoes/ =~ category => 8 8th characterFriday, March 4, 2011
  • SCAN >> numbers = "one two three" => "one two three" >> numbers.scan(/w+/) => ["one", "two", "three”]Friday, March 4, 2011
  • SPLIT WITH REGULAR EXPRESSIONS >> "one twotthree".split(/s/) => ["one", ”two", "three"]Friday, March 4, 2011
  • GSUB ”fred,mary,john".gsub(/fred/, “XXX”) => “XXX,mary,john”Friday, March 4, 2011
  • GSUB WITH A BLOCK "one twotthree".gsub(/(w+)/) do |w| ! puts w end one two threeFriday, March 4, 2011
  • TITLE CASE Capitalize All Words of a Sentence: >> full_name.gsub(/bw/){|s| s.upcase} => "Yukihiro Matsumoto"Friday, March 4, 2011