R 중급 정규 표현식
Angelo Zehr
Data Journalist
"cat"은 "c"로 시작하나요?
Rebus 방식:
str_detect("cat", pattern = START %R% "c")
정규 표현식:
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",
...
| 특수 문자 | 의미 |
|---|---|
^ |
캐럿: 행/문자열의 시작 표시 |
$ |
달러 기호: 행/문자열의 끝 표시 |
. |
마침표: 모든 문자(문자, 숫자, 공백) 일치 |
\\. |
백슬래시 2개: 실제 마침표 검색 시 이스케이프 |
| 코드 | 결과 |
|---|---|
str_match("Book", "^.") |
"B"와 일치 |
str_match("Book", ".$") |
"k"와 일치 |
str_match("Book", "\\.") |
일치 없음 |
str_match("Book.", "\\.") |
"."와 일치 |
R 중급 정규 표현식