-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path02-regex.rb
More file actions
71 lines (55 loc) · 2.04 KB
/
02-regex.rb
File metadata and controls
71 lines (55 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# regular expressions
# anything between two slashes is a regexp objecgt
# "=~" is ruby's regexp matching operator
# it returns the index of where a match is found
# nil is returned if there is no match
puts /is/ =~ "ruby is oo" # returns 5
# matches literal string
puts /c9/ =~ "my ide is c9"
# matches any string that starts with "a" and ends with "c"
puts /a.c/ =~ "abd aaa bbb abb abc" # matches "abc"
# for the literal . use \.
puts /a\.c/ =~ "a.c aaa bbb abb abc" # matches "a.c"
# modifier, i, allows for ignoring case
puts /dos/i =~ "uno DOS tres" # match "DOS"
# the !~ does the opposite of =~
puts /saturday/ !~ "caturday"
# strings come with sub() and gsub()
msg = "na na na na na na na na batman"
# sub() just changes the first occurence
puts msg.sub(/na/,"la")
# gsub() changes all occurences
puts msg.gsub(/na/,"la")
# sub!() makes changes to the string itself
puts msg.sub!(/na/,"la")
# gsub!()
puts msg.gsub!(/na/,"la")
puts msg
# character sets match charaters in brackets
word = "saturday"
puts word.gsub(/[aeiou]/,"x")
# the ^ symbol changes it to everythin but these chars
puts word.gsub(/[^aeiou]/,"x")
# short cut for commom sets
# \d any digit
# \D any non-digit
# \w any word character (letter, number, underscore)
# \W any non-word character
# \s any whitespace character (space, tab, newline)
# \S any non-whitespace character
# the split() method on string accepts regexs
# be careful not to forget the backslash character
puts "sundays are so relaxing".split(/\s+/).to_s
# + means at least one character
# ? means one or zero character
# * means zero or more characters
# the scan() method in strings returns the matches in an array
sentence = "I used to live in 30303 but now I live in 30043"
zipcodes = sentence.scan(/\d{5}/)
puts zipcodes.to_s
# the scan() method also accepts a block
zipcodes = sentence.scan(/\d{5}/) { |match| puts "A zipcode: #{match}" }
# use parentheses to capture groups
# scan() method returns an array of arrays
phone_nums = "678.407.5000 678.407.5001"
p phone_nums.scan(/(\d{3})\.(\d{3})\.(\d{4})/)