Expressions régulières intermédiaires en R
Angelo Zehr
Data Journalist
"cat" commence‑t‑il par "c" ?
La méthode 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 |
|---|---|
^ |
Accent circonflexe : marque le début d'une ligne ou d'une chaîne |
$ |
Signe dollar : marque la fin d'une ligne ou d'une chaîne |
. |
Point : correspond à tout caractère : lettres, chiffres ou espaces |
\\. |
Deux antislashs : échappent le point quand on cherche un vrai point |
| Code | Résultat |
|---|---|
str_match("Book", "^.") |
Correspond à "B" |
str_match("Book", ".$") |
Correspond à "k" |
str_match("Book", "\\.") |
Aucune correspondance |
str_match("Book.", "\\.") |
Correspond à "." |
Expressions régulières intermédiaires en R