Matthew Gaughan, Assignment 1 for Sociology 476: Computational Content Analysis.
library(stringr)
string_0 <- "Before you get started, I have to ask you for a quick favor. I lost my
key. Could you please search for it and grab it from this string here
using regex?"
string_1 <- "Ah, thanks! But, actually, that was the wrong key. Actually, it’s this
key that I need. I don’t need the first key. Can you get this one using
a single regex?"
string_2 <- "Given the confusion we’ve had with keys lately, I was wondering whether
it would make sense to use colored keys. For instance, we’d have a yellow
key, a purple key, and a red key, etc. And then you could just grab all
those at once with a single regex, but you wouldn’t accidentally get
some other key. Couldn’t you? By the way, I found my NU-ID - this purple
keycard I had been searching for so long."
#string 0
str_extract_all(string_0, regex("key"))
## [[1]]
## [1] "key"
#string 1
str_extract_all(string_1, regex("\\bkey\\b(?!\\.)"))
## [[1]]
## [1] "key"
#string 2
str_extract_all(string_2, regex("\\bkey\\b(?!\\.)"))
## [[1]]
## [1] "key" "key" "key"