從文字擷取匹配與周邊內容

R 中級 Regular Expressions

Angelo Zehr

Data Journalist

公司名稱的提及

「…到了店裡。體驗超級順暢無縫,物超所值。我非常推薦 ABC Enterprises,而且我一定會再回來!接著我們…」

一個單字:(\\w+\\s),0 到 10 個單字:(\\w+\\s){0,10}

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

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

R 中級 Regular Expressions

標點符號

「…到了店裡。體驗超級順暢無縫,物超所值。我非常推薦 ABC Enterprises,而且我一定會再回來!接著我們…」

擷取結果: "I would highly recommend ABC Enterprises and I will be coming back for"

\\w+ 換成 [\\w[:punct:]]+

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

R 中級 Regular Expressions

一起來練習吧!

R 中級 Regular Expressions

Preparing Video For Download...