Extracting matches and surroundings from a text

Intermediate Regular Expressions in R

Angelo Zehr

Data Journalist

Mentions of a company name

"...got to the store. Super smooth and seamless experience. Great value. I would highly recommend ABC Enterprises and I will be coming back for sure! Next, we went..."

One word: (\\w+\\s), 0 to 10 words: (\\w+\\s){0,10}

str_extract_all(
  blog_post,
  pattern = "(\\w+\\s){0,10}ABC Enterprises\\s?(\\w+\\s){0,10}"
)

Returns: "I would highly recommend ABC Enterprises and I will be coming back for"

Intermediate Regular Expressions in R

Punctuation

"...got to the store. Super smooth and seamless experience. Great value. I would highly recommend ABC Enterprises and I will be coming back for sure! Next, we went..."

Extracted: "I would highly recommend ABC Enterprises and I will be coming back for"

Replace \\w+ with [\\w[:punct:]]+

Extracted: "smooth and seamless experience. Great value. I would highly recommend ABC Enterprises and I will be coming back for sure! Next, we "

Intermediate Regular Expressions in R

Let's practice!

Intermediate Regular Expressions in R

Preparing Video For Download...