View source: R/fixed_regex_linter.R
fixed_regex_linter | R Documentation |
fixed=TRUE
in regular expressions where appropriateInvoking a regular expression engine is overkill for cases when the search pattern only involves static patterns.
fixed_regex_linter(allow_unescaped = FALSE)
allow_unescaped |
Logical, default |
NB: for stringr
functions, that means wrapping the pattern in stringr::fixed()
.
NB: this linter is likely not able to distinguish every possible case when a fixed regular expression is preferable, rather it seeks to identify likely cases. It should never report false positives, however; please report false positives as an error.
best_practices, configurable, efficiency, readability, regex
linters for a complete list of linters available in lintr.
# will produce lints
code_lines <- 'gsub("\\\\.", "", x)'
writeLines(code_lines)
lint(
text = code_lines,
linters = fixed_regex_linter()
)
lint(
text = 'grepl("a[*]b", x)',
linters = fixed_regex_linter()
)
lint(
text = 'grepl("a[*]b", x)',
linters = fixed_regex_linter(allow_unescaped = TRUE)
)
code_lines <- 'stringr::str_subset(x, "\\\\$")'
writeLines(code_lines)
lint(
text = code_lines,
linters = fixed_regex_linter()
)
lint(
text = 'grepl("Munich", address)',
linters = fixed_regex_linter()
)
# okay
code_lines <- 'gsub("\\\\.", "", x, fixed = TRUE)'
writeLines(code_lines)
lint(
text = code_lines,
linters = fixed_regex_linter()
)
lint(
text = 'grepl("a*b", x, fixed = TRUE)',
linters = fixed_regex_linter()
)
lint(
text = 'stringr::str_subset(x, stringr::fixed("$"))',
linters = fixed_regex_linter()
)
lint(
text = 'grepl("Munich", address, fixed = TRUE)',
linters = fixed_regex_linter()
)
lint(
text = 'grepl("Munich", address)',
linters = fixed_regex_linter(allow_unescaped = TRUE)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.