Weitere ähnliche Inhalte Mehr von Nell Shamrell-Harrington (20) Kürzlich hochgeladen (20) Beyond the Basics: Regular Expressions in Ruby1. BEYOND THE BASICS
Regular Expressions in Ruby
@nellshamrell
Thursday, February 7, 13
4. Test
Extract
Change
Thursday, February 7, 13
5. Test
Extract
Change
Thursday, February 7, 13
6. Test
Extract
Change
Thursday, February 7, 13
8. You program from a regular
expression to a result
Source: The Well Grounded Rubyist
Thursday, February 7, 13
21. string = “The force will be with
you always.”
Thursday, February 7, 13
22. string = “The force will be with
you always.”
m =/force/.match(string)
Thursday, February 7, 13
23. string = “The force will be with
you always.”
m =/force/.match(string)
=> #<MatchData “force” >
Thursday, February 7, 13
24. string = “The force will be with
you always.”
m =/force/.match(string,5)
Thursday, February 7, 13
25. string = “The force will be with
you always.”
m =/force/.match(string,5)
=> nil
Thursday, February 7, 13
26. What can you do with MatchData?
Thursday, February 7, 13
28. m.to_s
=> “force”
Thursday, February 7, 13
29. m.to_s
=> “force”
m.pre_match
Thursday, February 7, 13
30. m.to_s
=> “force”
m.pre_match
=> “The ”
Thursday, February 7, 13
31. m.to_s
=> “force”
m.pre_match
=> “The ”
m.post_match
Thursday, February 7, 13
32. m.to_s
=> “force”
m.pre_match
=> “The ”
m.post_match
=> “ be with you”
Thursday, February 7, 13
40. m[1]
=> “The ”
Thursday, February 7, 13
41. m[1]
=> “The ”
m[2]
Thursday, February 7, 13
42. m[1]
=> “The ”
m[2]
=> “ will be with you always ”
Thursday, February 7, 13
44. m[0]
=> “The force will be with
you always.”
Thursday, February 7, 13
47. m.each do |match|
puts match.upcase
end
=> NoMethodError
Thursday, February 7, 13
49. m.to_a.each do |match|
puts match.upcase
end
=> “THE FORCE WILL BE
WITH YOU ALWAYS”
“THE ”
“WILL BE WITH YOU
ALWAYS”
Thursday, February 7, 13
52. string = “Who’s the more
foolish? The fool or
the fool who follows
him?”
Thursday, February 7, 13
53. string = “Who’s the more
foolish? The fool or
the fool who follows
him?”
/fool/
Thursday, February 7, 13
64. Positive means a match for the
lookahead should be present
Thursday, February 7, 13
66. Assertion means the lookahead only
determines whether a match exists
Thursday, February 7, 13
67. string = “Who’s the more
foolish? The fool or
the fool who follows
him?”
Thursday, February 7, 13
69. Negative means a match for the
lookahead should not be present
Thursday, February 7, 13
76. string = “For my ally is the
force, and a powerful
ally it is”
Thursday, February 7, 13
77. string = “For my ally is the
force, and a powerful
ally it is”
/ally/
Thursday, February 7, 13
89. Greedy
Lazy
Possessive
Thursday, February 7, 13
90. Greedy
Lazy
Possessive
Thursday, February 7, 13
91. Greedy
Lazy
Possessive
Thursday, February 7, 13
94. +
/.+/
Thursday, February 7, 13
98. string = “This is no time to
talk about time we
don’t have the time”
Thursday, February 7, 13
99. string = “This is no time to
talk about time we
don’t have the time”
/.+time/
Thursday, February 7, 13
101. /.+time/.match(string)
=> “This is no time to
talk about time we
don’t have the time”
Thursday, February 7, 13
124. Nell Shamrell
Software Development Engineer
Blue Box Group
@nellshamrell
Thursday, February 7, 13