-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy path09-RegularExpressions.coffee
More file actions
192 lines (161 loc) · 4.45 KB
/
09-RegularExpressions.coffee
File metadata and controls
192 lines (161 loc) · 4.45 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require './prelude'
# Bring underscore into global
globalize _
# Slashes and escapes
show '--- Slashes and escapes ---'
slash = /\//
show 'AC/DC'.search slash
asteriskOrBrace = /[\{\*]/
story = 'We noticed the *giant sloth*, ' +
'hanging from a giant branch.';
show story.search asteriskOrBrace
# Special characters
show '--- Special characters ---'
digitSurroundedBySpace = /\s\d\s/
show '1a 2 3d'.search digitSurroundedBySpace
notABC = /[^ABC]/
show 'ABCBACCBBADABC'.search notABC
# Exercise 32
show '--- Exercise 32 ---'
datePattern = /\d\d\/\d\d\/\d\d\d\d/
show 'born 15/11/2003 (mother Spot): White Fang'\
.search datePattern
show '--- End of Exercise ---'
# Boundaries
show '--- Boundaries ---'
show /a+/.test 'blah'
show /^a+$/.test 'blah'
show /cat/.test 'concatenate'
show /\bcat\b/.test 'concatenate'
# Repetitions
show '--- Repetitions ---'
parenthesizedText = /\(.*\)/
show "Its (the sloth's) claws were gigantic!"\
.search parenthesizedText
datePattern = /\d{1,2}\/\d\d?\/\d{4}/
show 'born 15/11/2003 (mother Spot): White Fang'\
.search datePattern
datePattern = ///
\d{1,2} # day
/ # separator
\d\d? # month
/ # separator
\d{4} # year
///
show 'born 15/11/2003 (mother Spot): White Fang'\
.search datePattern
# Exercise 33
show '--- Exercise 33 ---'
mailAddress = /\b[\w\.-]+@[\w\.-]+\.\w{2,3}\b/
mailAddress = ///
\b[\w\.-]+ # username
@
[\w\.-]+\ # provider
.
\w{2,3}\b # domain
///
show mailAddress.test 'kenny@test.net'
show mailAddress.test 'I mailt kenny@tets.nets, ' +
'but it didn wrok!'
show mailAddress.test 'the_giant_sloth@gmail.com'
show '--- End of Exercise ---'
# Grouping
show '--- Grouping ---'
cartoonCrying = /boo(hoo+)+/i
show "Then, he exclaimed 'Boohoooohoohooo'"\
.search cartoonCrying
# Choice
show '--- Choice ---'
holyCow = /(sacred|holy) (cow|bovine|bull|taurus)/i
show holyCow.test 'Sacred bovine!'
# Match method
show '--- Match method ---'
show 'No'.match /Yes/
show '... yes'.match /yes/
show 'Giant Ape'.match /giant (\w+)/i
quote = "My mind is a swirling miasma " +
"(a poisonous fog thought to " +
"cause illness) of titilating " +
"thoughts and turgid ideas."
parenthesized = quote.match ///
(\w+) # Word
\s* # Whitespace
\((.*)\) # Explanation
///
if parenthesized isnt null
show "Word: #{parenthesized[1]} " +
"Explanation: #{parenthesized[2]}"
# Exercise 34
show '--- Exercise 34 ---'
extractDate = (string) ->
found = string.match /(\d\d?)\/(\d\d?)\/(\d{4})/
if found == null
throw new Error "No date found in '#{string}'."
new Date Number(found[3]),
Number(found[2]) - 1,
Number(found[1])
show extractDate \
"born 5/2/2007 (mother Noog): Long-ear Johnson"
show '--- End of Exercise ---'
# Replace method
show '--- Replace method ---'
show 'Borobudur'.replace /[ou]/g, 'a'
# Numbered parts
show '--- Numbered parts ---'
names = '''Picasso, Pablo
Gauguin, Paul
Van Gogh, Vincent'''
show names.replace /([\w ]+), ([\w ]+)/g, '$2 $1'
show names.replace ///
([\w ]+) # Lastname
,
([\w ]+) # Firstname
///g, '$2 $1'
# Replacement function
show '--- Replacement function ---'
eatOne = (match, amount, unit) ->
amount = Number(amount) - 1
if amount == 1
unit = unit.slice 0, unit.length - 1
else if amount == 0
unit = unit + 's'
amount = 'no'
amount + ' ' + unit
stock = '1 lemon, 2 cabbages, and 101 eggs'
stock = stock.replace /(\d+) (\w+)/g, eatOne
show stock
# Exercise 35
show '--- Exercise 35 ---'
escapeHTML = (text) ->
replacements = [[/&/g, '&']
[/"/g, '"']
[/</g, '<']
[/>/g, '>']]
forEach replacements, (replace) ->
text = text.replace replace[0], replace[1]
text
show escapeHTML '< " & " >'
escapeHTML = (text) ->
replacements =
"<": "<"
">": ">"
"&": "&"
"\"": """
text.replace /[<>&"]/g, (character) ->
replacements[character]
show escapeHTML "The 'pre-formatted' tag " +
"is written \"<pre>\"."
show '--- End of Exercise ---'
# Word filter
show '--- Word filter ---'
badWords = ['ape', 'monkey', 'simian',
'gorilla', 'evolution']
pattern = new RegExp badWords.join('|'), 'i'
isAcceptable = (text) ->
!pattern.test text
show isAcceptable 'Mmmm, grapes.'
show isAcceptable 'No more of that monkeybusiness, now.'
# Escape escapes
show '--- Escape escapes ---'
digits = new RegExp '\\d+'
show digits.test '101'