I have defined a function just.matches
that basically just combines regmatches
and gregexpr
into one function:
just.matches <- function (search.expression, corpus, ...) {
regmatches(corpus, gregexpr(search.expression, corpus, ...))
}
In a teaching context where we haven't gotten to regexes yet, I want to show that for some characters, one needs to prefix a double backslash because otherwise one will get an error. So I want to use this:
#| error: true
example <- "This is an example of a word in curly brackets: *cheese*."
just.matches("*", example) # doesn't work
But while I get the intended error in RStudio's console ...
Warning: PCRE pattern compilation error
'quantifier does not follow a repeatable item'
at ''Error in gregexpr(search.expr, corpus.v, perl = perl, ...) :
invalid regular expression ''
... when I knit the qmd, I get this instead:
[[1]]
[1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
[26] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
[51] "" "" "" "" "" "" ""
What am I doing wrong? Thanks a lot!