File tree 2 files changed +64
-0
lines changed
2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ content = 'The Princess And The Monkey
2
+
3
+ Once upon a time there was a princess...
4
+ ...and they all lived happily ever after.
5
+
6
+ The End'
7
+
8
+
9
+ # it matches the regexp at 29
10
+ /Once upon a time/ =~ content # 29
11
+
12
+ # starts with -> \A
13
+ /\A Once upon a time/ =~ content # nil
14
+
15
+ # ends with -> \z
16
+ /and they all lived happily ever after\z / =~ content # nil
17
+
18
+ # the circumflex ^ to match the beginning of the content or
19
+ # the beginning of any new line within the content
20
+ puts "Found it! at the beginning..." if content =~ /^Once upon a time/
21
+
22
+ # the dolar sign $ to match the ending of the content or
23
+ # the ending of any new line within the content
24
+ puts "Found it! at the ending..." if content =~ /ever after\. $/
25
+
26
+ # m modifier at the ending of the regular expresion turns on
27
+ # the match between lines
28
+ puts "catched!" if /^Once upon a time.*ever after\. $/m =~ content
Original file line number Diff line number Diff line change
1
+ the_time = '10:24 PM'
2
+
3
+ puts /\d \d :\d \d (AM|PM)/ =~ the_time # 0
4
+
5
+ puts /PM/ =~ the_time # 6
6
+
7
+
8
+ # if there is not match, it will return nil
9
+ may_match = /May/ =~ 'Sometime in June'
10
+ puts may_match . inspect
11
+
12
+
13
+ # =~ operator is ambidextrous
14
+ puts "It's not morning!" if /PM/ =~ the_time
15
+ puts "It's not morning!" if the_time =~ /PM/
16
+
17
+
18
+ # you can turn case sensitivity OFF
19
+ puts "It matches!" if /AM/i =~ 'am'
20
+
21
+
22
+ # obscure_times! method
23
+ class Document
24
+ attr_accessor :content
25
+
26
+ def initialize ( content )
27
+ @content = content
28
+ end
29
+
30
+ def obscure_times!
31
+ @content . gsub! ( /\d \d :\d \d (AM|PM)/i , '**:** **' )
32
+ end
33
+ end
34
+
35
+ oclock = Document . new ( '12:00 am' ) . obscure_times!
36
+ puts oclock
You can’t perform that action at this time.
0 commit comments