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.
This exercise is part of the course
String Manipulation with stringr in R
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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
___