-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex in R.R
91 lines (71 loc) · 2.4 KB
/
regex in R.R
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
#Introduction---------
library(tidyverse)
str_extract(fruit, "nana")
fruit#compare with fruit
?str_extract
x <- c("apple", "banana", "pear", "melon")
x <- c("apple", "banana", "pear")
str_extract(x, "a")
x <- c("a.b.c.d", "aeb")
starts_with <- "a.b"
str_detect(x, paste0("^", starts_with))
paste0("^", starts_with)
writeLines(paste0("^", starts_with))
str_c("|", x,"|", c("A", "b","c","D"))
?paste
paste0(1:12, c("st", "nd", "rd", rep("th", 9)))
x <- "a\\b"
str_view(x, "\\\\")
str_c(x, collapse = ",")
str_c(NA, collapse = ",")
function1 <- function(vect){if (length(vect)<=1) { #the function
print(vect)
} else if (length(vect)==2) {
str_c(vect, collapse = " and ")
} else if (length(vect)==3) {
paste0(str_c(vect[1:(length(vect)-1)], collapse = ", "), " and ", vect[3])
} else {
paste0(str_c(vect[1:(length(vect)-1)], collapse = ", ")," and ", vect[length(vect)])
}}
fruit
function1()
# The draft of functions
if (length(x)<=1) {
print(x)
} else if (length(x)==2) {
str_c(x, collapse = " and ")
} else if (length(x)==3) {
paste0(str_c(x[1:(length(x)-1)], collapse = ", "), " and ", x[3])
} else {
paste0(str_c(x[1:(length(x)-1)], collapse = ", ")," and ", x[length(x)])
}
x <- "\"\'\\"
writeLines(x)
str_view(x, "\"\'\\\\") # " and ' do not have specific meaning in regex
x<-".af."
str_view(x, "\\.")
#14.3.2 Anchors-----------
x <- "$^$"
writeLines(x)
str_view(x, "\\$\\^\\$") # ^ and $ have special meaning in regex, thus need \\ rather than \
str_view(words, "^y")
random <- sample(1:980, 20)
str_view(words[random], "^qu")
str_view(words[random], "[abc]") #start with a,b or c
str_view(words[random], "[^abc]") #start without a,b or c
# Look for a literal character that normally has special meaning in a regex
str_view(c("abc", "a.c", "a*c", "a c"), "a[.]c")
str_view(c("abc", "a.c", "a*c", "a c"), ".[*]c")
str_view(c("abc", "a.c", "a*c", "a c"), "a[ ]", match = T) #equal to str_view(c("abc", "a.c", "a*c", "a c"), "a[ ]")
str_view(c("grey", "gray"), "gr(e|a)y")
str_view(words[300:500], "^(a|e|i|o|u)", match = F)
str_view(words, "ed$", match =T)
?str_view
x <- "1888 is the longest year in Roman numerals: MDCCCLXXXVIII"
str_view(x, "CC+")
str_view(x, "CC?")
str_view(x, 'C[LX]+')
str_view(x, 'C[LX]?')
str_view(x, 'C[LX]') #matches CL or CX
str_view(words[random], "^.*$") #Include all strings
str_view"(word, "\\\\")