CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

String Manipulation with stringr in R

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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
___
Modifier et exécuter le code