Reg EX

713 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
713
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Reg EX

  1. 1. REGULAR EXPRESSIONS /regex will find me/Friday, March 4, 2011
  2. 2. 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
  3. 3. REGULAR EXPRESSIONS IN RUBYFriday, March 4, 2011
  4. 4. 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
  5. 5. REGULAR EXPRESSION SYNTAX • Regular expressions are put between two forward slashes (/match_me/) • They are escaped with a backward slash ().Friday, March 4, 2011
  6. 6. CHARACTERS THAT NEED TO BE ESCAPED . | ( ) [ ] { } + ^ $ * ?Friday, March 4, 2011
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. SCAN >> numbers = "one two three" => "one two three" >> numbers.scan(/w+/) => ["one", "two", "three”]Friday, March 4, 2011
  13. 13. SPLIT WITH REGULAR EXPRESSIONS >> "one twotthree".split(/s/) => ["one", ”two", "three"]Friday, March 4, 2011
  14. 14. GSUB ”fred,mary,john".gsub(/fred/, “XXX”) => “XXX,mary,john”Friday, March 4, 2011
  15. 15. GSUB WITH A BLOCK "one twotthree".gsub(/(w+)/) do |w| ! puts w end one two threeFriday, March 4, 2011
  16. 16. TITLE CASE Capitalize All Words of a Sentence: >> full_name.gsub(/bw/){|s| s.upcase} => "Yukihiro Matsumoto"Friday, March 4, 2011

×