管線與問號

R 中級 Regular Expressions

Angelo Zehr

Data Journalist

這個或那個

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 中級 Regular Expressions

讓部分成為可選

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

str_view 的結果

R 中級 Regular Expressions

貪婪 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 中級 Regular Expressions

一起來練習吧!

R 中級 Regular Expressions

Preparing Video For Download...