【问题标题】:Selecting the word immediately after a keyword在关键字之后立即选择单词
【发布时间】:2015-05-01 11:17:17
【问题描述】:

我正在尝试使用 R 立即将单词提取为关键字。我对正则表达式没有太多经验,因此到目前为止我发现的所有内容都对我没有多大帮助。如果我可以让函数返回多个实例,那将是理想的。

例如,如果我的关键字是 the 而我的字符串是:

The yellow log is in the stream

它将返回 yellowstream

我找到了这个solution for c#,它看起来和我想要的完全一样,但是我在 R 中实现它时遇到了麻烦。

【问题讨论】:

  • 尝试显示您目前拥有的一些代码。

标签: regex r


【解决方案1】:

你可以试试

library(stringr)
str_extract_all(str1, perl('(?<=\\b(?i)The )\\w+'))[[1]]
#[1] "yellow" "stream"

或使用stringi

library(stringi)
stri_extract_all_regex(str1, '(?<=\\b(?i)The )\\w+')[[1]]
 #[1] "yellow" "stream"

编辑:根据@Roland 在 cmets 中的建议进行了更改。

数据

str1 <- 'The yellow log is in the stream'

【讨论】:

  • 谢谢,由于某种原因,这给出了“perl is deprecated”警告。
  • @brknparticle 我正在使用stringr_0.6.2,但没有收到任何警告
  • @brknparticle 可以试试我刚刚更新的stringi 方法吗
【解决方案2】:

key 分配给您想要使用的任何字符串

key <- 'the'
p <- "The yellow log is in the stream" 
regmatches(p, gregexpr(sprintf('(?i)(?<=%s\\s)\\w+', key), p, perl = TRUE))[[1]]
# [1] "yellow" "stream"

或者正如@Roland 指出的那样,在关键字周围使用单词边界来避免这种情况会更安全:

key <- 'the'
p <- "The yellow log is in the stream drinking absinthe and beer"
regmatches(p, gregexpr(sprintf('(?i)(?<=%s\\s)\\w+', key), p, perl = TRUE))[[1]]
# [1] "yellow" "stream" "and"   

regmatches(p, gregexpr(sprintf('(?i)(?<=\\b%s )\\w+', key), p, perl = TRUE))[[1]]
# [1] "yellow" "stream"

【讨论】:

  • 我会使用"(?&lt;=(\\b[Tt]he ))\\w+"。边界一词在这里似乎是必要的。
  • 谢谢,这正是我所需要的。最后的 [[1]] 是做什么的?
  • @brknparticle regmatches 正在解释来自gregexpr 的匹配数据列表,因此它返回一个列表。您只需选择列表的第一个元素即可获得匹配项
【解决方案3】:

这里是非regex的解决方案:

mytext <- "The yellow log is in the stream"
mykey <- "the"

x <- unlist(strsplit(mytext," "))

x[which(tolower(x)==mykey)+1]

【讨论】:

  • 这段代码对我来说实际上很有意义,所以我选择了它作为最好的。谢谢。
  • 请注意,这也将返回紧跟在键之后的单词的任何标点符号(因此可能需要在例如列表词频之前进一步清理)。
【解决方案4】:

试试这个:这会返回“黄色”和“流”

x <- "The yellow log is in the stream"

regmatches(x, gregexpr("(?:(?:T|t)he)\\s(\\w+)", x, perl = TRUE))[[1]]
## [1] "The yellow" "the stream"

【讨论】:

    【解决方案5】:

    我维护的 qdapRegex 包在 regex_supplement 字典中有一个正则表达式 after_ 非常适合这个。您可以使用rm_ 制作自己的after_the 函数:

    library(qdapRegex)
    
    x<- "The yellow log is in the stream"
    after_the <- rm_(pattern = S("@after_", "[Tt]he"), extract = TRUE)
    after_the(x)
    
    ## [[1]]
    ## [1] "yellow" "stream"
    

    S 函数是 sprintf 的包装器,可让您轻松地将元素(如本例中的“the”)传递给生成的基本正则表达式:

    S("@after_", "the", "The")
    ## [1] "(?<=\\b(the|The)\\s)(\\w+)"
    

    编辑

    library(qdapRegex)
    
    x<- c("The yellow log is in the stream", "I like the one box for a pack")
    after_ <- rm_(extract = TRUE)
    after_the(x)
    
    after_ <- rm_(extract = TRUE)
    
    words <- c("the", "a", "one")
    
    setNames(lapply(words, function(y){
        after_(x, pattern = S("@after_", y, TC(y)))
    }), words)
    
    
    ## $the
    ## $the[[1]]
    ## [1] "yellow" "stream"
    ## 
    ## $the[[2]]
    ## [1] "one"
    ## 
    ## 
    ## $a
    ## $a[[1]]
    ## [1] NA
    ## 
    ## $a[[2]]
    ## [1] "pack"
    ## 
    ## 
    ## $one
    ## $one[[1]]
    ## [1] NA
    ## 
    ## $one[[2]]
    ## [1] "box"
    

    【讨论】:

    • 这听起来很有趣。我可以在这里传递一个关键字列表,例如c("the", "a", "one") 并在每个关键字之后选择它吗?
    • 查看我对这种方法的编辑。你还是想loop
    • 好的,很好。我已经解决了我的问题,但这看起来是一种非常优雅的方法。我会看看是否可以让它为我工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多