Expressions régulières intermédiaires en R
Angelo Zehr
Data Journalist
"cat" commence-t-il par "c" ?
Façon « rebus » :
str_detect("cat", pattern = START %R% "c")
Expression régulière :
str_detect("cat", pattern = "^c")
str_detect(string, pattern)
str_match(string, pattern)


movie_titles <- c(
"Karate Kid",
"The Twilight Saga: Eclispe",
"Knight & Day",
"Shrek Forever After (3D)",
"Marmaduke.",
"Predators",
"StreetDance (3D)",
"Robin Hood",
"Micmacs A Tire-Larigot",
"Sex And the City 2",
...
movie_titles[
str_detect(
movie_titles,
pattern = "^K"
)
]
"Karate Kid",
"Knight & Day",
...
| Caractère spécial | Signification |
|---|---|
^ |
« Caret » : début de ligne ou de chaîne |
$ |
« Dollar sign » : fin de ligne ou de chaîne |
. |
Point : correspond à tout : lettres, chiffres ou espaces |
\\. |
Deux barres obliques inverses : échappe le point pour chercher un vrai point |
| Code | Résultat |
|---|---|
str_match("Book", "^.") |
Fait correspondre "B" |
str_match("Book", ".$") |
Fait correspondre "k" |
str_match("Book", "\\.") |
Aucune correspondance |
str_match("Book.", "\\.") |
Fait correspondre "." |
Expressions régulières intermédiaires en R