Capturing parts of a pattern
In rebus, to denote a part of a regular expression you want to capture, you surround it with the function capture(). For example, a simple pattern to match an email address might be,
email <- one_or_more(WRD) %R%
"@" %R% one_or_more(WRD) %R%
DOT %R% one_or_more(WRD)
str_view("([email protected])", pattern = email)
If you want to capture the part before the @, you simply wrap that part of the regular expression in capture():
email <- capture(one_or_more(WRD)) %R%
"@" %R% one_or_more(WRD) %R%
DOT %R% one_or_more(WRD)
str_view("([email protected])", pattern = email)
The part of the string that matches hasn't changed, but if we pull out the match with str_match() we get access to the captured piece:
str_match("([email protected])", pattern = email)
You'll explore this behavior with some more super hero email addresses.
이 연습은 강의의 일부입니다
String Manipulation with stringr in R
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Capture parts between @ and . and after .
email <- capture(one_or_more(WRD)) %R%
"@" %R% one_or_more(WRD) %R%
DOT %R% one_or_more(WRD)
# Check match hasn't changed
___