fork download
  1. regex.escape <- function(string) {
  2. gsub("([][{}()+*^$|\\\\?.])", "\\\\\\1", string)
  3. }
  4. BuildRegex <- function(search) {
  5. x<-strsplit(search,"\\s+")
  6. paste0("^(?=.*(?<!\\w)", paste(regex.escape(unlist(x)), collapse="(?!\\w))(?=.*(?<!\\w)"), "(?!\\w))")
  7. }
  8. BuildRegex("John Tolkien")
  9. grepl(BuildRegex("John Tolkien"), "John Ronald Reuel Tolkien", perl=TRUE)
  10. grepl(BuildRegex("Tolkien John"), "John Ronald Reuel Tolkien", perl=TRUE)
  11. grepl(BuildRegex("John Ronald Tolkien"), "John Ronald Reuel Tolkien", perl=TRUE)
Success #stdin #stdout 0.22s 39744KB
stdin
Standard input is empty
stdout
[1] "^(?=.*(?<!\\w)John(?!\\w))(?=.*(?<!\\w)Tolkien(?!\\w))"
[1] TRUE
[1] TRUE
[1] TRUE