【问题标题】:Regex negative lookbehind in RR中的正则表达式负回顾
【发布时间】:2016-10-26 00:30:29
【问题描述】:

所以基本上,我有一个看起来像这样的文本数据:

See item 7 Management's Discussion and Analysis. BlahBlahBlah. Item 7 Management's Discussion and Analysis. BlahBlahBlah. Item 8 Financial Statements and Supplementary Data.

我想从“blahblahblah”之后的“Item 7”中选择所有内容。对“第8项-财务报表及补充数据”的句子

所以我想要

Item 7 Management's Discussion and Analysis. BlahBlahBlah. Item 8 Financial Statements and Supplementary Data.

除了包含“参见第 7 项管理层的讨论和分析”的句子之外的所有内容

现在,我正在使用以下代码:

(?<!see)Item 7(.*?)Item 8 

但它没有返回我想要的。

我的逻辑是不要查看包含单词“see”后跟“item 7 Management's Discussion and Analysis”的句子,但它似乎不起作用。

https://regex101.com/r/yF7aQ1/3

有没有一种方法可以实现这种消极的后视?

【问题讨论】:

    标签: r regex negative-lookbehind


    【解决方案1】:

    不确定您是如何在 R 中实现它的,.*(?&lt;!See) (item 7 .*)sub 一起使用,请注意 see 后面的空格和可以使用 ignore.case 参数忽略的字母大小写。

    sub(".*(?<!See) (item 7 .*)", "\\1", s, ignore.case = T, perl = T)
    
    # [1] "Item 7 Management's Discussion and Analysis. BlahBlahBlah. Item 8 Financial Statements and Supplementary Data."
    

    另一种选择:

    sub(".*(?=(?<!See) ?item 7)", "", s, ignore.case = T, perl = T)
    # [1] "Item 7 Management's Discussion and Analysis. BlahBlahBlah. Item 8 Financial Statements and Supplementary Data."
    

    使用来自stringr 包的str_extract_all(),它似乎没有提供ignore.case 选项,您可以使用[Ii] 忽略大小写:

    library(stringr)
    str_extract_all(s, "(?<!See )[Ii]tem 7(.*)")
    # [1] "Item 7 Management's Discussion and Analysis. BlahBlahBlah. Item 8 Financial Statements and Supplementary Data."
    

    【讨论】:

    • 哦!对不起!我忘了补充说我正在使用 stringr 来做这件事。
    • 所以 str_extract_all(text, regex("(?
    • 是的,你可以。查看更新。您的正则表达式似乎与您想要的输出不匹配。我正在关注您原始问题的输出。
    • 太棒了!我会试试的。并且 ignore.case 适用于 str_extract 我只需要这样说: str_extract_all(text, regex("(?
    • 所以如果我正确理解了您的正则表达式,它会排除任何包含“see”和“item 7”的句子吗?
    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2012-02-27
    • 2015-12-10
    • 1970-01-01
    • 2021-03-21
    • 2015-11-21
    相关资源
    最近更新 更多