Exercise

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.

Instructions 1/2

undefined XP
    1
    2
  • Add some more capture() calls to the email pattern, to capture the part after the @ but before the .; and the part after the ..
  • Check that the whole email address is matched by using str_view() on hero_contacts with the email pattern.