From Markdown Mapper, which is GPLv2-licensed (but release this section under the Public Domain? – so Markdown Mapper overall is still GPLv2, but this can be used however):

# We may want, e.g., to use 'ignore.case = TRUE' for gsub below, which means NOT using 'fixed = TRUE'. So we need to escape all of the characters in the 'From' column that could be interpreted as regular expression characters:
deactivate_regular_expression_special_characters <- function(string_to_sanitize){
list_of_regular_expression_symbols_to_escape <- c( # Following the list at http://stackoverflow.com/a/9310752/1940466
'[', # Note: for some reason, '\\]' makes the search that uses this vector below stop working, so I'm not using it here.
'-',
'\\',
'{',
'}',
'(',
')',
'*',
'+',
'?',
'.',
',',
'^',
'\$',
'|'
)

list_of_regular_expression_symbols_to_escape.collapsed <- paste(list_of_regular_expression_symbols_to_escape, sep= "", collapse = '\\')
list_of_regular_expression_symbols_to_escape.collapsed <- paste('\\', list_of_regular_expression_symbols_to_escape.collapsed, sep = "") # Add '\\' before the first element in the list above, since it would have been missed by our last paste(sep='\\') command.

string.sanitized <- gsub(paste('([', list_of_regular_expression_symbols_to_escape.collapsed, '])', sep = ""), '\\\\\\1', string_to_sanitize)

return(string.sanitized)
}


Next
Previous