fork download
  1. x <- c("lv function is reduced", "lv function is not reduced", "reduced lv function", "no evidence of reduced lv function")
  2.  
  3. cap <- "reduced|depressed|normal"
  4. negate_prefix <- paste0("(?:\\b(?:no|not|none)\\b\\D*?\\b(?:",cap,")\\b\\D*?")
  5. match <- "\\b(?:lv function|lv|systolic function|left ventricular ejection fraction)\\b"
  6. regex <- paste0(negate_prefix,
  7. match, "|", match, "\\D*?\\bnot\\b\\D*?\\b(?:",cap,")\\b)(*SKIP)(*F)|(?:\\b(",cap,")\\b\\D*?)?",match,"(?:\\D*?\\b(",cap,")\\b)?")
  8. #cat(regex, collapse="\n") # => (?:\b(?:no|not|none)\b\D*?\b(?:reduced|depressed|normal)\b\D*?\b(?:lv function|lv|systolic function|left ventricular ejection fraction)\b|\b(?:lv function|lv|systolic function|left ventricular ejection fraction)\b\D*?\bnot\b\D*?\b(?:reduced|depressed|normal)\b)(*SKIP)(*F)|(?:\b(reduced|depressed|normal)\b\D*?)?\b(?:lv function|lv|systolic function|left ventricular ejection fraction)\b(?:\D*?\b(reduced|depressed|normal)\b)?
  9. results <- regmatches(x, regexec(regex, x, perl=TRUE))
  10. unlist(lapply(results, function(x) paste(x[-1], collapse="")))
Success #stdin #stdout 0.24s 39644KB
stdin
Standard input is empty
stdout
[1] "reduced" ""        "reduced" ""