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.
Diese Übung ist Teil des Kurses
String Manipulation with stringr in R
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# 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
___