【问题标题】:Get the three word with regexpr [closed]使用 regexpr 获取三个单词 [关闭]
【发布时间】:2013-10-30 13:39:32
【问题描述】:

我只能得到第一个Adam,开始位置,结束位置,长度,我怎样才能得到文本中的另外两个亚当?

text <- c("Hellow, Adam!", "Hi, Adam!", "How are you, Adam.")
regexpr("Adam", text)

【问题讨论】:

  • 你的意思是gregexpr
  • regexpr 获取所有信息。 gregexpr 也是如此。
  • 请更具体。你的预期输出是什么?你看过regexpr的例子吗?

标签: regex r


【解决方案1】:

它工作得很好,为每个“亚当”输出起始索引和长度:

> text <- c("Hellow, Adam!", "Hi, Adam!", "How are you, Adam.")    
> regexpr("Adam", text)
[1]  9  5 14
attr(,"match.length")
[1] 4 4 4
attr(,"useBytes")

[1] TRUE

regexpr 的输出是一个结构体,一个带有附加属性的起始位置的整数向量 (?attributes)。

x <- regexpr("Adam", text)
c(x[2], attributes(x)$match.length[2])

获取第二个匹配的起始位置和字符串长度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多