Ruby RegEx

2,694 views

Published on

Ruby RegEx by Liah Hansen

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

No Downloads
Views
Total views
2,694
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
30
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • s finds spaces, tabs and new lines
  • Ruby RegEx

    1. 1. /regex will find me/<br />Regular Expressions<br />
    2. 2. What are Regular Expressions<br />Regular expressions allow matching and manipulation of textual data.<br />Abbreviated as regex or regexp, or alternatively, just patterns<br />
    3. 3. Regular Expressions in Ruby<br />
    4. 4. Using Regular Expressions <br />Scan a string for multiple occurrences of a pattern.<br />Replace part of a string with another string.<br />Split a string based on a matching separator.<br />
    5. 5. Regular Expression Syntax<br /><ul><li> Regular expressions are put between two forward slashes (/match_me/)
    6. 6. They are escaped with a backward slash ().</li></li></ul><li>Characters That Need to be Escaped<br />. | ( ) [ ] { } + ^ $ * ?<br />
    7. 7. Regex Basics<br />[abc] A single character: a, b or c <br />[^abc] Any single character but a, b, or c <br />[a-z] Any single character in the range a-z <br />[a-zA-Z] Any single character in the range a-z or A-Z <br />^ Start of line <br />$ End of line <br />A Start of string <br />z End of string<br />
    8. 8. Regex Basics cont...<br />. Any single character <br />s Any whitespace character <br />S Any non-whitespace character <br />d Any digit <br />D Any non-digit <br />w Any word character (letter, number, underscore) <br />W Any non-word character <br /> Any word boundary character<br />
    9. 9. Regex Basics cont...<br />(...) Capture everything enclosed <br />(a|b) a or b <br />a? Zero or one of a <br />a* Zero or more of a <br />a+ One or more of a <br />a{3} Exactly 3 of a <br />a{3,} 3 or more of a <br /> a{3,6} Between 3 and 6 of a<br />
    10. 10. Regex: .match<br />>> category = "power tools"<br />=> "power tools"<br />>> puts "on Sale" if category.match(/power tools/)<br />on Sale<br />>> puts "on Sale" if /power tools/.match(category)<br />on Sale<br />
    11. 11. Regex: =~<br />>> category = "shoes"<br />=> "shoes"<br />>> puts "15 % off" if category =~ /shoes/<br />15 % off<br />>> puts "15 % off" if /shoes/ =~ category<br />15 % off<br />>> /pants/ =~ category<br />=> nil<br />>> /shoes/ =~ category<br />=> 0<br />>> category = "women's shoes”<br />>> /shoes/ =~ category<br />=> 8<br />8th character<br />
    12. 12. Scan<br />>> numbers = "one two three"<br />=> "one two three"<br />>> numbers.scan(/w+/)<br />=> ["one", "two", "three”]<br />
    13. 13. Split with Regular Expressions<br />>> "one two three".split(/s/)<br />=> ["one", ”two", "three"]<br />
    14. 14. gsub<br />”fred,mary,john".gsub(/fred/, “XXX”)<br />=> “XXX,mary,john”<br />
    15. 15. gsub with a block<br />"one two three".gsub(/(w+)/) do |w| puts w<br />end<br />one<br />two<br />three<br />
    16. 16. Title Case<br />Capitalize All Words of a Sentence:<br />>> full_name.gsub(/w/){|s| s.upcase}<br />=> "Yukihiro Matsumoto"<br />

    ×