The function cat_re()
prints a regular expression to the console.
By default, the regular expression is not printed as an R string,
but as a `plain regular expression'. More specifically, the regular expression
is printed without surrounding quotation marks, and characters that are
special characters in R strings (such as quotation marks and backslashes)
are not escaped with a backslash. Also, by default, multi-line regular expressions are
printed as single-line regular expressions with all regular expression comments removed.
Usage
cat_re(x, format = c("plain", "R"), as_single_line = TRUE)
Arguments
- x
An object of class
re
or a character vector containing a regular expression. Ifx
is a character vector of length higher than 1, only its first element will be used.- format
Character vector describing the requested format (as a
"plain"
regular expression or as an"R"
string). If its length is higher than 1, only its first element will be used.- as_single_line
Logical. Whether
x
should be converted to a single line regular expression, therefore also removing all comments, prior to printing. If the length of this vector is larger than 1, only its first item will be used.
Details
WARNING: In the current implementation, the way the character #
is handled is
not guaranteed to be correct. More specifically, the code is not guaranteed
to correctly distinguish between a #
symbol that introduces a regular
expression comment and a #
symbol that doesn't do so. Firstly,
there is no testing whether at the point of encountering #
we're in
free-spacing mode. Second, there is no thorough testing whether or not
the #
symbol is part of a character class.
However, #
is processed correctly as long as any 'literal #' is
immediately preceded by either a backslash or an opening square bracket,
and any `comment-introducing #' is not immediately preceded by
a backslash or an opening square bracket.
Examples
# single-line regular expression
x <- "(?xi) \\b \\w* willing \\w* \\b"
cat_re(x)
#> (?xi) \b \w* willing \w* \b
#>
y <- "(?xi)
\\b # word boundary
\\w* # optional prefix
willing # stem
\\w* # optional suffix
\\b # word boundary"
cat_re(y)
#> (?xi) \b \w* willing \w* \b
#>
cat_re(y, as_single_line = FALSE)
#> (?xi)
#> \b # word boundary
#> \w* # optional prefix
#> willing # stem
#> \w* # optional suffix
#> \b # word boundary
#>
cat_re(y, format = "R")
#> "(?xi) \\b \\w* willing \\w* \\b"
#>
cat_re(y, format = "R", as_single_line = FALSE)
#> "(?xi)
#> \\b # word boundary
#> \\w* # optional prefix
#> willing # stem
#> \\w* # optional suffix
#> \\b # word boundary"
#>
regex <- re("(?xi)
\\b # word boundary
\\w* # optional prefix
willing # stem
\\w* # optional suffix
\\b # word boundary")
cat_re(regex)
#> (?xi) \b \w* willing \w* \b
#>
cat_re(regex, as_single_line = FALSE)
#> (?xi)
#> \b # word boundary
#> \w* # optional prefix
#> willing # stem
#> \w* # optional suffix
#> \b # word boundary
#>