パイプとクエスチョンマーク

Rで学ぶ中級正規表現

Angelo Zehr

Data Journalist

A または B

lines <- c(
  "Karate Kid 2, Distributor: Columbia, 58 Screens",
  "Finding Nemo, Distributors: Pixar and Disney, 10 Screens",
  "Finding Harmony, Distributor: Unknown, 1 Screen",
  "Finding Dory, Distributors: Pixar and Disney, 8 Screens"
)
str_detect(lines, "Columbia|Pixar")
TRUE TRUE FALSE TRUE
Rで学ぶ中級正規表現

任意指定にする

str_view(lines, pattern = "Distributor|Distributors")
str_view(lines, pattern = "Distributors?")

str_view の結果

Rで学ぶ中級正規表現

貪欲 vs. 遅延

str_view("Toy Story 3 In Disney Digital 3D", ".*3")

str_view の結果(貪欲)

str_view("Toy Story 3 In Disney Digital 3D", ".*?3")

str_view の結果(遅延)

Rで学ぶ中級正規表現

練習しましょう!

Rで学ぶ中級正規表現

Preparing Video For Download...