q1_ans <- majors %>%
filter(grepl("DATA", Major) | grepl("STATISTICS", Major))
q1_ans
## # A tibble: 3 × 3
## FOD1P Major Major_Category
## <chr> <chr> <chr>
## 1 6212 MANAGEMENT INFORMATION SYSTEMS AND STATISTICS Business
## 2 2101 COMPUTER PROGRAMMING AND DATA PROCESSING Computers & Mathematics
## 3 3702 STATISTICS AND DECISION SCIENCE Computers & Mathematics
#Loads fruit list
fruit_list <- '[1] "bell pepper" "bilberry" "blackberry" "blood orange"
[5] "blueberry" "cantaloupe" "chili pepper" "cloudberry"
[9] "elderberry" "lime" "lychee" "mulberry"
[13] "olive" "salal berry"'
patternf <- "\"(.*?)\""
q2_ans <- str_extract_all(fruit_list, patternf)
# Turns the list info a vector
q2_ans <- unlist(q2_ans)
q2_ans <- gsub('\\"', "", q2_ans)
print(q2_ans)
## [1] "bell pepper" "bilberry" "blackberry" "blood orange" "blueberry"
## [6] "cantaloupe" "chili pepper" "cloudberry" "elderberry" "lime"
## [11] "lychee" "mulberry" "olive" "salal berry"
(.)\1\1 = matches any sequence of a single character repeating three times. (ex: “aaa”)
“(.)(.)\2\1” = matches any sequences of characters that starts out with two characters, and is then followed by a repetition of those two characters, but in reverse order, which looks like this (abba, cddc, effe). and the two characters have to be in between “”
(..)\1 = matches any sequences of two characters, where the sequence repeats, which looks like this (abab, cdcd, efef)
“(.).\1.\1” = matches any sequences of characters where the starting character repeats three times, and those occurrences are separated by any character. All of this has to be between ““. Something which looks like this (adaxa, ckchc, nlnjn, lmlnl)
“(.)(.)(.).*\3\2\1” = matches any sequences of characters where it begins with a set of three characters, followed by some character (or none)and then ending with the first set of three characters, but in reverse order. All of this between “”
#For testing
text <- 'abccbalkjsdoihfoie'
pattern1 <- '(.)(.)(.).*\\3\\2\\1'
q3_ans <- regmatches(text, gregexpr(pattern1, text))
q3_ans
## [[1]]
## [1] "abccba"
#For testing
text <- 'bobo bo kokoko'
regex_1 <- "\\b(\\w)\\w*\\1\\b"
#//b denotes the limits of the word
#(\\w) denotes the first character of the word
#\\w* represents all following character
#\\1 means that the last character should be the same as the first character
regex_2 <- "\\b\\w*(\\w\\w)\\w*\\1\\w*\\b"
#//b denotes the limits of the word
#\\w* denotes any character (or none) before the two sets of character
#(\\w\\w) represents the two characters
regex_3 <- "\\b\\w*(\\w)\\w*\\1\\w*\\1\\w*\\b"
q4_ans <- regmatches(text, gregexpr(regex_3, text))
q4_ans
## [[1]]
## [1] "kokoko"