# BACKREFERENCES #####~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# Parenthesized expressions in a regex can be referred "back" to # with \1, \2 ... # (remember in R you need two backslashes - i.e. \\1, \\2, ...)## The original regex standard only allowed for up to nine# backreferences, ie. \1 \2 \3 ... \9 # It did not allow for \10. Some environments have ways to# allow you to reference \10 and further but I personally# don't know how to do that in R ... I guess you could # research that if you need to but it usually doesn't# come up. If it becomes an issue, there is almost always# a simple way to workaround the situation# using loops and other coding approaches.#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# Find fruits that have 3 letters in the pattern xyx or abagrep ("([a-z])[a-z]\\1", fruit, value=TRUE)
# QUESTION # Find fruits that start and end with the same letter#grep("^(.).*\\1$", fruit, value=TRUE)
[1] "strawberries" "yumberry"
shoppingList =c("35 yumberry pops", "four strawberries ", " five apples","six yumberry and strawberries pops")shoppingList
[1] "35 yumberry pops" "four strawberries "
[3] " five apples" "six yumberry and strawberries pops"
# QUESTION# Use sub or gsub to replace words that start and end with the same letter# with the first letter then "XXXX" then the last letter of the wordgsub("\\b(.).*\\1\\b", "\\1XXXX\\1", shoppingList)