【问题标题】:In R, extract text between headings using regular expressions在 R 中,使用正则表达式提取标题之间的文本
【发布时间】:2020-05-28 13:11:57
【问题描述】:

我想提取章节标题之间的所有文本,包括第一个/开始标题,但不包括结束标题。标题始终为大写字母,始终以数字-句点或数字-字母-句点组合开头,并始终后跟空格/s。我想保留副标题(即“6.1”、“7A.1”)作为提取字符串的一部分。这是一些示例文本:

example <- "5. SCOPE This document outlines what to do in case of emergency landing (ignore for non-emergency landings) on tarmac. 6. WHEELS Never land on tarmac. Unless you have lowered the plane wheel mechanism. 6.1 Lower the wheel mechanism using the switch labelled 'wheel mechanism'. 7A WARNING 7A.1 Do not forget to warn passengers."

# The output I want is:

"5. SCOPE This document outlines what to do in case of emergency landing (ignore for non-emergency landings) on tarmac."

"6. WHEELS Never land on tarmac. Unless you have lowered the plane wheel mechanism. 6.1 Lower the wheel mechanism using the switch labelled 'wheel mechanism'."

"7A WARNING 7A.1 Do not forget to warn passengers."

使用stringr 包,并在post 的帮助下,我走到了这一步:

library(stringr)
str_extract_all(example, "(\\d+\\w?\\.?[:blank:]+[:upper:]+)(.*?)(?=\\d+\\w?\\.?[:blank:]+[:upper:]+)")

# Explanation of my regex code:
# (\\d+\\w?\\.?[[:blank:]]+[[:upper:]])
# \\d+   one or more digits
# \\w?   zero or one letter
# \\.?   zero or one period
# [:blank:]+   one or more space/tab
# [:upper]+    one or more capital letters

# (.*?)   non-greedy capture, zero or one or more of any character

# (?=\\d+\\w?\\.?[:blank:]+[:upper:]+)
# ?=   followed by
# \\d+   one or more digits
# \\w?   zero or one letter
# \\.?   zero or one period
# [:blank:]+   one or more space/tab
# [:upper]+    one or more capital letters

这非常接近我想要的,只有两件事出了问题。第一个是“6.1”它分裂成“6”。和“1”。第二个是最后一章标题之后的文本没有被捕获,看起来它可能会像“6.1”一样被拆分:

[[1]]
[1] "5. SCOPE This document outlines what to do in case of emergency landing (ignore for non-emergency landings) on tarmac. "
[2] "6. WHEELS Never land on tarmac. Unless you have lowered the plane wheel mechanism. 6."                                  
[3] "1 Lower the wheel mechanism using the switch labelled 'wheel mechanism'. "                                              
[4] "7A WARNING 7A."  

我哪里错了??

【问题讨论】:

    标签: r regex stringr


    【解决方案1】:

    你可以使用

    example <- "5. SCOPE This document outlines what to do in case of emergency landing (ignore for non-emergency landings) on tarmac. 6. WHEELS Never land on tarmac. Unless you have lowered the plane wheel mechanism. 6.1 Lower the wheel mechanism using the switch labelled 'wheel mechanism'. 7A WARNING 7A.1 Do not forget to warn passengers."
    
    library(stringr)
    str_split(example, "(?!^)(?<!\\d[.A-Z])(?<!\\d[A-Z]\\.)\\b(?=\\d+(?:[a-zA-Z]|\\.)\\s+\\p{Lu})")
    

    输出:

    [[1]]
    [1] "5. SCOPE This document outlines what to do in case of emergency landing (ignore for non-emergency landings) on tarmac. "                                       
    [2] "6. WHEELS Never land on tarmac. Unless you have lowered the plane wheel mechanism. 6.1 Lower the wheel mechanism using the 2 Switch labelled 'wheel mechanism'. "
    [3] "7A WARNING 7A.1 Do not forget to warn passengers."                                                                                                             
    

    请参阅R demoregex demo

    详情

    • (?!^) - 不在字符串的开头
    • (?&lt;!\d[.A-Z]) - 如果前面有数字和点或字母,则不是
    • (?&lt;!\d[A-Z]\.) - 如果前面有数字、字母、点,则不是
    • \b - 匹配一个单词边界位置...
    • (?=\d+(?:[a-zA-Z]|\.)\s+\p{Lu}) - 后跟 1+ 位数字,然后是字母或点,然后是 1+ 空格和大写字母。

    【讨论】:

    • 谢谢,几乎可以了!它仍然会在我的实际数据中出现数字空格大写字母的实例,例如6.1 Lower the wheel mechanism using the 2 Switches labelled 'wheel mechanism'. 输出为6.1 Lower the wheel mechanism using the " "2 Switches labelled 'wheel mechanism'. "\\p{Lu} 有关吗?
    • @mendy 如果数字后面必须有点或字母,请使用(?!^)(?&lt;!\d[.A-Z])(?&lt;!\d[A-Z]\.)\b(?=\d+(?:[a-zA-Z]|\.)\s+\p{Lu}),见this regex demo
    【解决方案2】:

    这也有效:

    str_extract_all(example, "\\d[.A-Z\\d\\s]+[A-Z]{2,}[\\s(.\\w]+")
    [[1]]
    [1] "5. SCOPE This document outlines what to do in case of emergency landing (ignore for non"                                                    
    [2] "6. WHEELS Never land on tarmac. Unless you have lowered the plane wheel mechanism. 6.1 Lower the wheel mechanism using the switch labelled "
    [3] "7A WARNING 7A.1 Do not forget to warn passengers."
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 2015-05-17
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多