Basic Regular Expressions

323 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
323
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Basic Regular Expressions

  1. 1. Regular Expressions What are they and what can they do for you?Thursday, February 7, 13
  2. 2. b[A-Z0-9._%+-]+@[A- Z0-9.-]+.[A-Z]{2-4}b Source: http:/ /www.regular- expressions.info/email.htmlThursday, February 7, 13
  3. 3. So...what is a regular expression?Thursday, February 7, 13
  4. 4. A Regular Expression is a pattern for a string.Thursday, February 7, 13
  5. 5. What can you do with Regular Expressions? Test a string Extract a string Change a stringThursday, February 7, 13
  6. 6. Creating Regular Expressions /RegularExpression/Thursday, February 7, 13
  7. 7. Scenario #1 Testing StringsThursday, February 7, 13
  8. 8. Examples from “Programming Ruby” (PickAxe)Thursday, February 7, 13
  9. 9. “dog and cat”Thursday, February 7, 13
  10. 10. “dog and cat” /cat/Thursday, February 7, 13
  11. 11. “dog and cat” /cat/ /cat/ =~ “dog and cat”Thursday, February 7, 13
  12. 12. =~ /cat/ =~ “dog and cat”Thursday, February 7, 13
  13. 13. Thursday, February 7, 13
  14. 14. Thursday, February 7, 13
  15. 15. match /cat/.match(“dog and cat”)Thursday, February 7, 13
  16. 16. Valid EmailsThursday, February 7, 13
  17. 17. thing@thing.thingThursday, February 7, 13
  18. 18. RubularThursday, February 7, 13
  19. 19. Alternation /Nell | Brandon/Thursday, February 7, 13
  20. 20. . Metacharacters any single character character can appear * any number of timesThursday, February 7, 13
  21. 21. /.*/ matches ANYTHINGThursday, February 7, 13
  22. 22. Range [a-d] [1-4]Thursday, February 7, 13
  23. 23. Shorthand! w stands for any word character. It’s the same as: [a-zA-Z0-9_]Thursday, February 7, 13
  24. 24. Metacharacters + character occurs one or more timesThursday, February 7, 13
  25. 25. Thursday, February 7, 13
  26. 26. Scenario #2 Extracting StringsThursday, February 7, 13
  27. 27. Shorthand! d stands for any digit D stands for any non- digitThursday, February 7, 13
  28. 28. Repetition! d{3} Looks for exactly 3 digitsThursday, February 7, 13
  29. 29. Make It Optional! ? Makes a character optional. It can occur 0 or 1 time.Thursday, February 7, 13
  30. 30. Scenario #3 Changing StringsThursday, February 7, 13
  31. 31. “Brandon is the teacher of the class right now. Brandon is teaching about regular expressions.”Thursday, February 7, 13
  32. 32. sub(/pattern/, “text”)Thursday, February 7, 13
  33. 33. gsub(/pattern/, “text”) (replace ALL the matches)Thursday, February 7, 13
  34. 34. sub and gsub create new strings. sub! and gsub! change the original stringsThursday, February 7, 13
  35. 35. Welcome to the wonderful world of Regular Expressions!Thursday, February 7, 13

×