【问题标题】:Execute gsub after first instance of closing character instead of continuing to end of string [duplicate]在第一个结束字符实例之后执行 gsub,而不是继续到字符串结尾 [重复]
【发布时间】:2018-09-07 18:02:10
【问题描述】:

我有一个数据集,其中列是调查问题,行中的值包含响应者选择的答案以及多个 HTML 标记。我正在尝试删除所有 HTML 标签,只留下答案文本。

在 Excel 中,这可以通过使用空字符串作为替换来完成<*>。我无法弄清楚如何在 R 中执行此操作,因为我遇到的问题是我无法让通配符在第一个大于括号之后停止。相反,它只是将其识别为通配符的一部分并继续到字符串的末尾。我在下面包含了一个玩具数据集和我的尝试。

temp <- data.frame(one = c('<b style="font-weight: normal;"><span style="font-size: 12pt; font-family: "Times New Roman";white-space: pre-wrap;">Answer 1</span></b>',
                         '<b style="font-weight: normal;"><span style="font-size: 12pt; font-family: "Times New Roman";white-space: pre-wrap;">Answer 2</span></b>',
                         '<b style="font-weight: normal;"><span style="font-size: 12pt; font-family: "Times New Roman";white-space: pre-wrap;">Answer 3</span></b>'),
                   two = c('<b style="font-weight: normal;"><span style="font-size: 12pt; font-family: "Times New Roman";white-space: pre-wrap;">apples are red</span></b>',
                         '<b style="font-weight: normal;"><span style="font-size: 12pt; font-family: "Times New Roman";white-space: pre-wrap;">apples are blue</span></b>',
                         '<b style="font-weight: normal;"><span style="font-size: 12pt; font-family: "Times New Roman";white-space: pre-wrap;">apples are bananas</span></b>'))


temp[] <- sapply(temp, function(x) gsub('<.*>+', "", x))

# what I want the new temp to look like (above code results in empty strings
data.frame(one = c("Answer 1", 
                   "Answer 2", 
                   "Answer 3"),
           two = c("apples are red",
                   "apples are blue", 
                   "apples are bananas

我尝试使用第 n 次出现的代码和其他一些代码,但它仍然在第一个实例之后继续到字符串的末尾。

我缺少什么使其在第一个实例后终止的正则表达式命令?另外,我假设它会在完成第一次删除后移动到下一行,从而迫使我运行gsub() n 次,其中 n 是任何给定列中的最大标签数。这不是特别成问题,但有解决方法吗?

【问题讨论】:

    标签: r regex gsub


    【解决方案1】:

    查看regex 文档的摘录:

    默认情况下重复是贪心的,所以最大可能的次数 使用重复。可以通过将? 附加到 量词。 (还有更多的量词允许近似 匹配:参见 TRE 文档。)

    temp[] <- sapply(temp, function(x) gsub('<.*?>', "", x))
    
           one                two
    1 Answer 1     apples are red
    2 Answer 2    apples are blue
    3 Answer 3 apples are bananas
    

    为了回答您的第二个问题,gsub 将替换所有匹配项(而不是 sub,它只替换第一个匹配项) - 所以您应该没问题。

    【讨论】:

    • 为什么要使用 sapply? gsub 被矢量化并保持维度。就做gsub('&lt;.*?&gt;', "", as.matrix(temp))
    【解决方案2】:

    使用str_extract,我们可以提取&gt;&lt;之间的单词字符和空格:

    library(stringr)
    library(dplyr)
    
    temp %>%
      mutate_all(str_extract, "(?<=\\>)[\\w\\s]+(?=\\<)")
    

    输出:

           one                two
    1 Answer 1     apples are red
    2 Answer 2    apples are blue
    3 Answer 3 apples are bananas
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多