Exercise

Matching the start or end of the string

rebus provides START and END shortcuts to specify regular expressions that match the start and end of the string. These are also known as anchors. You can try it out just by typing

START

You'll see the output <regex> ^. The <regex> denotes this is a special regex object and it has the value ^. ^ is the character used in the regular expression language to denote the start of a string.

The special operator provided by rebus, %R% allows you to compose complicated regular expressions from simple pieces. When you are reading rebus code, think of %R% as "then". For example, you could combine START with c,

START %R% "c"

to match the pattern "the start of string then a c", or in other words: strings that start with c. In rebus, if you want to match a specific character, or a specific sequence of characters, you simply specify them as a string, e.g. surround them with ".

Instructions 1/4

undefined XP
  • 1
    • Which character is used to match the end of a string? Print END to find out.
    • str_view() from stringr is really helpful for testing your patterns. Run this line of code to see the matches in x to the pattern START %R% "c".
  • 2

    Provide the pattern argument to match strings that start in "co".

  • 3

    Provide the pattern argument to match strings that end in "at". Think about this as matching an "at" followed by the end of the string.

  • 4

    Provide the pattern argument to match strings that are "cat" exactly. Think about this as matching the start of the string, followed by "cat" and then the end of the string.