Escape whitespace in regular expression patterns

escapeWhitespaceRegexp(x, whitespace = "_ ", maxN = 20, ...)

Arguments

x

character vector containing a regular expression pattern, or a character value that should be converted to a regular expression pattern.

whitespace

character vector length=1 containing whitespace characters, for example " _" will define space " " and underscore "_" both as whitespace characters.

maxN

integer value for maximum iterations of the substitution, unfortunately the regular expression logic only matches once per iteration per string.

...

additional arguments are ignored.

Details

This function is intended to test for unescaped whitespace characters in a regular expression pattern match string, and replace them with escaped whitespace characters, possibly expanding the allowed whitespace characters in the process.

See also

Other jam string functions: sortSamples(), strsplitOrdered()

Examples

x <- c("one two three", "one[ ]two three", "one[ 12] two three"); escapeWhitespaceRegexp(x);
#> [1] "one[_ ]+two[_ ]+three" "one[ ]two[_ ]+three" #> [3] "one[ 12][_ ]+two[_ ]+three"
# side-by-side summary of input and output data.frame(input=paste0('"', x, '"'), output=paste0('"', escapeWhitespaceRegexp(x), '"'))
#> input output #> 1 "one two three" "one[_ ]+two[_ ]+three" #> 2 "one[ ]two three" "one[ ]two[_ ]+three" #> 3 "one[ 12] two three" "one[ 12][_ ]+two[_ ]+three"