正则表达式 – 提取字符向量中两个特定单词之间的所有单词

前端之家收集整理的这篇文章主要介绍了正则表达式 – 提取字符向量中两个特定单词之间的所有单词前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有更有效的方法吗?如果没有字符串,我怎么能这样做?
  1. txt <- "I want to extract the words between this and that,this goes with that,this is a long way from that"
  2.  
  3. library(stringr)
  4. w_start <- "this"
  5. w_end <- "that"
  6. pattern <- paste0(w_start,"(.*?)",w_end)
  7. wordsbetween <- unlist(str_extract_all(txt,pattern))
  8. gsub("^\\s+|\\s+$","",str_sub(wordsbetween,nchar(w_start)+1,-nchar(w_end)-1))
  9. [1] "and" "goes with" "is a long way from"
这是我在qdap中使用的方法

使用qdap:

  1. library(qdap)
  2. genXtract(txt,"this","that")
  3.  
  4. ## > genXtract(txt,"that")
  5. ## this : that1 this : that2 this : that3
  6. ## " and " " goes with " " is a long way from "

没有添加包:

  1. regmatches(txt,gregexpr("(?<=this).*?(?=that)",txt,perl=TRUE))
  2.  
  3. ## > regmatches(txt,perl=TRUE))
  4. ## [[1]]
  5. ## [1] " and " " goes with " " is a long way from "

猜你在找的正则表达式相关文章