r - 如何提取最长的匹配?

标签 r regex stringr purrr

考虑这个简单的例子

library(stringr)
library(dplyr)

dataframe <- data_frame(text = c('how is the biggest ??',
                                 'really amazing stuff'))

# A tibble: 2 x 1
  text                 
  <chr>                
1 how is the biggest ??
2 really amazing stuff 


我需要基于regex表达式提取一些术语,但仅提取最长的术语。

到目前为止,我只能使用str_extract提取第一个匹配项(不需要最长的匹配项)。

> dataframe %>% mutate(mymatch = str_extract(text, regex('\\w+')))
# A tibble: 2 x 2
  text                  mymatch
  <chr>                 <chr>  
1 how is the biggest ?? how    
2 really amazing stuff  really 


我尝试使用str_extract_all,但找不到有效的语法。
输出应为:

# A tibble: 2 x 2
  text                  mymatch
  <chr>                 <chr>  
1 how is the biggest ?? biggest
2 really amazing stuff  amazing 


有任何想法吗?
谢谢!

最佳答案

您可以执行以下操作:

library(stringr)
library(dplyr)

dataframe %>%
  mutate(mymatch = sapply(str_extract_all(text, '\\w+'), 
                          function(x) x[nchar(x) == max(nchar(x))][1]))


使用purrr

library(purrr)

dataframe %>%
  mutate(mymatch = map_chr(str_extract_all(text, '\\w+'), 
                           ~ .[nchar(.) == max(nchar(.))][1]))


结果:

# A tibble: 2 x 2
                   text mymatch
                  <chr>   <chr>
1 how is the biggest ?? biggest
2  really amazing stuff amazing


注意:

如果有平局,则采用第一个。

数据:

dataframe <- data_frame(text = c('how is the biggest ??',
                                 'really amazing biggest stuff'))

关于r - 如何提取最长的匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50453844/

相关文章:

php - 仅使用 RegEx 将字符串拆分为固定大小的 block

string - 用 R 获取逗号前的字符串

r - as.numeric 函数更改我的数据框中的值

R: cut 是执行此操作的正确功能吗?

r - 匹配并提取r中的子字符串

仅删除换行符之间的空格

r - 在 R 中使用正则表达式检测一类字符中的一个或两个重复字符

r - 如何在 R 中分组时创建排名列

java - 在java中使用正则表达式从长字符串中提取特定值或子字符串

regex - 从正则表达式重定向中排除目录