Contenu connexe
Plus de Nell Shamrell-Harrington
Plus de Nell Shamrell-Harrington (14)
Regular Expressions Guide
- 2. b[A-Z0-9._%+-]+@[A-
Z0-9.-]+.[A-Z]{2-4}b
Source: http:/ /www.regular-
expressions.info/email.html
Thursday, February 7, 13
- 5. What can you do with
Regular Expressions?
Test a string
Extract a string
Change a string
Thursday, February 7, 13
- 7. Scenario #1
Testing Strings
Thursday, February 7, 13
- 8. Examples from
“Programming Ruby” (PickAxe)
Thursday, February 7, 13
- 11. “dog and cat”
/cat/
/cat/ =~ “dog and cat”
Thursday, February 7, 13
- 12. =~
/cat/ =~ “dog and cat”
Thursday, February 7, 13
- 15. match
/cat/.match(“dog and cat”)
Thursday, February 7, 13
- 19. Alternation
/Nell | Brandon/
Thursday, February 7, 13
- 20. .
Metacharacters
any single character
character can appear
* any number of times
Thursday, February 7, 13
- 21. /.*/
matches ANYTHING
Thursday, February 7, 13
- 22. Range
[a-d] [1-4]
Thursday, February 7, 13
- 23. Shorthand!
w stands for any word
character.
It’s the same as:
[a-zA-Z0-9_]
Thursday, February 7, 13
- 24. Metacharacters
+ character occurs
one or more times
Thursday, February 7, 13
- 26. Scenario #2
Extracting Strings
Thursday, February 7, 13
- 27. Shorthand!
d stands for any digit
D stands for any non-
digit
Thursday, February 7, 13
- 28. Repetition!
d{3}
Looks for exactly 3 digits
Thursday, February 7, 13
- 29. Make It Optional!
?
Makes a character
optional. It can occur
0 or 1 time.
Thursday, February 7, 13
- 30. Scenario #3
Changing Strings
Thursday, February 7, 13
- 31. “Brandon is the teacher
of the class right now.
Brandon is teaching about
regular expressions.”
Thursday, February 7, 13
- 34. sub and gsub create new
strings.
sub! and gsub! change
the original strings
Thursday, February 7, 13
- 35. Welcome to the
wonderful world of
Regular Expressions!
Thursday, February 7, 13