suppressPackageStartupMessages(library("tidyverse"))
package 㤼㸱tidyverse㤼㸲 was built under R version 3.6.3
?
, +
, *
in {m,n}
form.Pattern {m,n} Meaning ? {0,1} Match at most 1 + {1,} Match 1 or more * {0,} Match 0 or more
Pattern | {m,n} |
Meaning |
---|---|---|
? |
{0,1} |
Match at most 1 |
+ |
{1,} |
Match 1 or more |
* |
{0,} |
Match 0 or more |
For example, let’s repeat the examples in the lecture, replacing ? with
{0,1},
+with
{1,}, and
*with
{0,}`.
x <- "1888 is the longest year in Roman numerals: MDCCCLXXXVIII"
str_view(x, "CC?")
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
str_view(x, "CC{0,1}")
str_view(x, "CC+")
str_view(x, "CC{1,}")
str_view_all(x, "C[LX]+")
str_view_all(x, "C[LX]{0,1}")
The lecture does not contain an example of *
. This pattern looks for a “C” optionally followed by any number of “L” or “X” characters.
str_view_all(x, "C[LX]*")
str_view_all(x, "C[LX]{0,}")
^.*$
"\\{.+\\}"
\d{4}-\d{2}-\d{2}
"\\\\{4}"
The answer to each part follows.
^.*$
will match any string. For example: ^.*$
: c("dog", "$1.23", "lorem ipsum")
.
"\\{.+\\}"
will match any string with curly braces surrounding at least one character. For example: "\\{.+\\}"
: c("{a}", "{abc}")
.
\d{4}-\d{2}-\d{2}
will match four digits followed by a hyphen, followed by two digits followed by a hyphen, followed by another two digits. This is a regular expression that can match dates formatted like “YYYY-MM-DD” (“%Y-%m-%d”). For example: \d{4}-\d{2}-\d{2}
: 2018-01-11
"\\\\{4}" is \\{4}
, which will match four backslashes. For example: "\\\\{4}"
: "\\\\\\\\"
.
Start with three consonants. Have three or more vowels in a row. Have two or more vowel-consonant pairs in a row. The answer to each part follows.
1. Start with three consonants.
str_view(words, "^[^aeiou]{3}", match = TRUE)
2. Have three or more vowels in a row.
str_view(words, "[aeiou]{3,}", match = TRUE)
3. Have two or more vowel-consonant pairs in a row.
str_view(words, "([aeiou][^aeiou]){2,}", match = TRUE)
Visit https://regexcrossword.com/challenges/. It’s fun.