首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何迭代匹配单词序列

如何迭代匹配单词序列
EN

Stack Overflow用户
提问于 2020-07-08 11:42:40
回答 2查看 64关注 0票数 2

我有这样的条件:

代码语言:javascript
复制
test <- c("oh i mean well i do n't know well he 's like oh",
          "yeah so well he did n't say oh he said f** well you know what he 's like",
          "oh you know well why well maybe he thought oh well good", 
          "oh my god well what the hell did he oh you know")

我想匹配所有的单词序列,以oh开头,以well结尾,倒数,以well开头,以oh结尾。str_extract_all的这种使用确实匹配了一些目标序列,但不是全部,因为它无法迭代地进行匹配,也就是说,一旦在匹配中使用了每个ohwell,它就不会从每个ohwell重新开始:

代码语言:javascript
复制
library(stringr)
strings <- unlist(str_extract_all(test, "\\boh\\b.*?\\bwell\\b|\\bwell\\b.*?\\boh\\b"))
[1] "oh i mean well"           "well he 's like oh"       "well he did n't say oh"   "oh you know well"        
[5] "well maybe he thought oh" "oh my god well" 

全部产出如下:

代码语言:javascript
复制
[1] "oh i mean well"     "well he 's like oh"     "well he did n't say oh"     "oh he said f** well" 
[5] "oh you know well"  "oh well"   "well maybe he thought oh"     "oh my god well"
[9] "well what the hell did he oh" 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-08 11:57:41

您可以使用stringr::str_match_all解决方案(因为stringr::str_extract_all“丢失”了所有捕获的子字符串):

代码语言:javascript
复制
test <- c("oh i mean well i do n't know well he 's like oh",
"yeah so well he did n't say oh he said f** well you know what he 's like", 
"oh you know well why well maybe he thought oh well good",
"oh my god well what the hell did he oh you know")
res <- stringr::str_match_all(test, "(?=(\\boh\\b(?:(?!\\boh\\b).)*?\\bwell\\b|\\bwell\\b(?:(?!\\bwell\\b).)*?\\boh\\b))")

unlist(lapply(res, function(x) x[,-1]))

参见R演示在线regex演示

详细信息

  • (?= -积极展望的开始:
    • ( -捕获组的开始:
      • \boh\b(?:(?!\boh\b).)*?\bwell\b - oh整词,然后是任何0+字符,尽可能少地不把一个完整的单词oh开头,直到最左边的well整词。
      • | -或
      • \bwell\b(?:(?!\bwell\b).)*?\boh\b - well整词,然后是任何0+字符,尽可能少地不把一个完整的单词well开头,直到最左边的oh整词。

代码语言:javascript
复制
- `)` - end of the capturing group
  • ) -正面展望的结束。

输出:

代码语言:javascript
复制
[1] "oh i mean well"               "well he 's like oh"          
[3] "well he did n't say oh"       "oh he said f** well"         
[5] "oh you know well"             "well maybe he thought oh"    
[7] "oh well"                      "oh my god well"              
[9] "well what the hell did he oh"
票数 1
EN

Stack Overflow用户

发布于 2020-07-08 11:59:29

您可以使用str_extract_all,一个用于oh...well,另一个用于well...oh,使用正则表达式:

代码语言:javascript
复制
library(stringr)
unlist(c(str_extract_all(test, "\\boh\\b.*?\\bwell\\b")
       , str_extract_all(test, "\\bwell\\b.*?\\boh\\b")))
#[1] "oh i mean well"                       
#[2] "oh he said f** well"                  
#[3] "oh you know well"                     
#[4] "oh well"                              
#[5] "oh my god well"                       
#[6] "well i do n't know well he 's like oh"
#[7] "well he did n't say oh"               
#[8] "well why well maybe he thought oh"    
#[9] "well what the hell did he oh"         

或者如果是最短的序列:

代码语言:javascript
复制
unlist(c(str_extract_all(test, "\\boh\\b((?!\\boh\\b).)*?\\bwell\\b")
 , str_extract_all(test, "\\bwell\\b((?!\\bwell\\b).)*?\\boh\\b")))
#[1] "oh i mean well"               "oh he said f** well"         
#[3] "oh you know well"             "oh well"                     
#[5] "oh my god well"               "well he 's like oh"          
#[7] "well he did n't say oh"       "well maybe he thought oh"    
#[9] "well what the hell did he oh"

数据:

代码语言:javascript
复制
test <- c("oh i mean well i do n't know well he 's like oh",
          "yeah so well he did n't say oh he said f** well you know what he 's like",
          "oh you know well why well maybe he thought oh well good", 
          "oh my god well what the hell did he oh you know")
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62794046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档