-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.Rmd
306 lines (202 loc) · 7.17 KB
/
index.Rmd
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
---
title: 'Regular Expressions and stringr'
subtitle: 'Pavitra Chakravarty'
author: 'R-Ladies Cologne, R-Ladies Gaborone'
output:
xaringan::moon_reader:
lib_dir: libs
css: xaringan-themer.css
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
cache = TRUE,
cache.lazy = FALSE,
include = TRUE,
message = FALSE,
warning = FALSE
)
```
```{r xaringan-themer, include = FALSE}
library(xaringanthemer)
style_mono_light(
base_color = "#3092FF",
header_font_google = google_font("Josefin Sans"),
text_font_google = google_font("Montserrat", "300", "300i"),
code_font_google = google_font("Droid Mono"),
)
```
<style>
hide {
display: none;
}
.remark-slide-content h1 {
font-size: 45px;
}
h1 {
font-size: 2em;
margin-block-start: 0.67em;
margin-block-end: 0.67em;
}
.remark-slide-content {
font-size: 16px
}
.remark-code {
font-size: 14px;
}
code.r {
font-size: 14px;
}
pre {
margin-top: 0px;
margin-bottom: 0px;
}
.red {
color: #FF0000;
}
.footnote {
color: #800020;
font-size: 9px;
}
</style>
### What are regular expressions?
+ Regular expression is a pattern that describes a specific set of strings with a common structure
+ Heavily used for string matching / replacing in all programming languages
+ Heart and soul for string operations
---
### Regular expression syntax
6 basic canonical characteristics of regular expressions
+ __basic pattern matching__: Using functions from stringr package with exact sequence of characters
+ `str_detect()`, `str_subset()`, `str_view()`, `str_view_all()`
+ __anchors__: Indicate start and stop of sentence
+ `^: indicating start of sentence`, `$: indicating end of sentence`
+ __escape characters__: special characters cannot be directly coded in string
+ `\`: if you want to find strings with single quote `'`, "escape" single quote by preceding it with `\`
---
+ __character classes__: specify entire classes of characters, such as numbers, letters, etc using either `[:` and `:]` around predefined name or `\` and a special character
+ `[:digit:]` or `\d`: digits, 0 1 2 3 4 5 6 7 8 9, equivalent to `[0-9]`
+ `\D`: non-digits, equivalent to `[^0-9]`
+ `[:lower:]`: lower-case letters, equivalent to `[a-z]`
+ `[:upper:]`: upper-case letters, equivalent to `[A-Z]`
+ `[:alpha:]`: alphabetic characters, equivalent to `[[:lower:][:upper:]]` or `[A-z]`
+ `[:alnum:]`: alphanumeric characters, equivalent to `[[:alpha:][:digit:]]` or `[A-z0-9]`
+ `\w`: word characters, equivalent to `[[:alnum:]_]` or `[A-z0-9_]`
+ `\W`: not word, equivalent to `[^A-z0-9_]`
+ `[:blank:]`: blank characters, i.e. space and tab
* `[:space:]`: space characters: tab, newline, vertical tab, form feed, carriage return, space
* `\s`: space, ` `
* `\S`: not space
+ __quantifiers__: Quantifiers specify how many repetitions of the pattern
+ `*`: matches at least 0 times
+ `+`: matches at least 1 times
+ `?`: matches at most 1 times
+ `{n}`: matches exactly n times
+ `{n,}`: matches at least n times
+ `{n,m}`: matches between n and m times
+ __character clusters__: Use of paranthesis to keep pattern together
+ `()`: use with pattern-matching characters to create groups
---
### Dataset being used today
```{r dataset, eval=TRUE}
library(tidyverse)
enron <- read_csv("https://raw.githubusercontent.com/UBC-STAT/stat545.stat.ubc.ca/master/content/data/enron/enron.csv") %>% drop_na()
glimpse(enron)
head(enron, n=50)
```
---
### Canonical principle #1: Basic pattern-matching
```{r str_detect, eval=TRUE}
enron %>% filter(str_detect(enron$person, "Allen"))
```
```{r str_subset, eval=TRUE}
str_subset(enron$email, "tracy.ngo")
```
```{r str_view_all, eval=FALSE}
str_view_all(enron$email, "tracy.ngo")
```
---
### Canonical principle #2: Anchors
+ `^`: matches the start of the string.
+ `$`: matches the end of the string.
+ `\b`: matches the empty string at either edge of a _word_. Don't confuse it with `^ $` which marks the edge of a _string_.
+ `\B`: matches the empty string provided it is not at an edge of a word.
```{r str_start_anchor, eval=TRUE}
enron %>% filter(str_detect(enron$email, "@ECT")) %>% select
```
```{r str_end_anchor, eval=TRUE}
enron %>% filter(str_detect(enron$email, "weekend$"))
```
---
### Canonical principle #3: Escape characters
```{r escape_back, eval=TRUE}
x <- c("123-456-7890", "(123)456-7890", "(123) 456-7890", "1235-2351")
str_view(x, "(\\d\\d\\d)\\d\\d\\d-\\d\\d\\d\\d")
```
---
```{r escape_dollar, eval=TRUE}
str_view("so it goes $^$ here", "\\$\\^\\$")
```
---
### Canonical principle #4: Character Classes
```{r chr_class_1, eval=TRUE}
str_view(stringr::words, "^[yx]", match=TRUE)
```
---
```{r chr_class_2, eval=TRUE}
str_view(stringr::words, "[^e]ed$", match = TRUE)
```
---
```{r chr_class_3, eval=TRUE}
str_view(c("red", "reed"), "[^e]ed$", match = FALSE)
```
---
```{r chr_class_4, eval=TRUE}
str_view(stringr::words, "^(thr)*", match = TRUE)
```
### Canonical principle #5: Quantifiers
+ `*`: matches at least 0 times.
+ `+`: matches at least 1 times.
+ `?`: matches at most 1 times.
+ `{n}`: matches exactly n times.
+ `{n,}`: matches at least n times.
+ `{n,m}`: matches between n and m times.
```{r quant_1, eval=TRUE}
x <- c("dkl kls. klk. _", "(425) 591-6020", "her number is (581) 434-3242", "442", " dsi")
str_view(x, "^[dkh]*$")
```
---
```{r quant_2, eval=TRUE}
x <- c("123-456-7890", "(123)456-7890", "(123) 456-7890", "1235-2351")
str_view(x, "\\([0-9][0-9][0-9]\\)[ ]*[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]")
```
```{r quant_3, eval=TRUE}
x <- c("123-456-7890", "(123)456-7890", "(123) 456-7890", "1235-2351")
str_view(x, "\\([0-9][0-9][0-9]\\)[ ]+[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]")
```
---
```{r quant_4, eval=TRUE}
x <- c("123456-7890", "(123) 456-7890", "(123)456-7890", "1235-2351")
str_view(x, "\\([0-9][0-9][0-9]\\)[ ]?[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]")
```
---
```{r quant_5, eval=TRUE}
x <- c("4444-22-22", "test", "333-4444-22")
str_view(x, "\\d{4}-\\d{2}-\\d{2}")
```
---
### Canonical principle #6: Character Clusters
```{r cc_1, eval=TRUE}
enron %>% filter(str_detect(email, "@.*\\.(edu|net)")) %>% select(email)
```
```{r cc_2, eval=TRUE}
enron %>% filter(str_detect(email, "@.*(ns)\\.(net)")) %>% select(email)
```
---
### Lets Play!
https://regexcrossword.com/challenges/beginner/puzzles/1
---
### Acknowledgements
Material has been borrowed heavily from the STAT 545 course. This course was started by Jenny Bryan: https://stat545.stat.ubc.ca/notes/notes-b05/
More STAT 545 resources: https://stat545.com/character-vectors.html, https://youtu.be/I0dJ1zpxAtU
R for Data Science chapter on Strings: https://r4ds.had.co.nz/strings.html
Solution set for R4DS on Strings: https://brshallo.github.io/r4ds_solutions/14-strings.html#matching-patterns-w-regex
Regex Puzzle Builder: https://regexcrossword.com/puzzlebuilder