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 "
.
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.
# Some strings to practice with
x <- c("cat", "coat", "scotland", "tic toc")
# Print END
# Run me
str_view(x, pattern = START %R% "c")