【问题标题】:Find expression with spaces in R在R中查找带空格的表达式
【发布时间】:2018-07-04 09:01:03
【问题描述】:

从这样的数据框,

data_frame <- data.frame(com=c("chiffres au 31/12/2015","chiffre au 31/12/2008","chiffres au 31/12/08","xxx","abcd"))

我想提取模式为 "chiffres"+white_space+"au"+white_space+"dd/mm/yyyy" 的所有表达式

我已经尝试过这种模式,但它不起作用:

data_frame$date_plus_chiffre=str_extract(string=data_frame$com,
                               pattern="(chiffres)\s(au)\s([0-9]{2})(/)([0-9]{2})(/)([0-9]{4})")

如何在正则表达式中指定空格?

感谢您的帮助。

【问题讨论】:

  • ?regex 状态:[:blank:] Blank characters: space and tab, and possibly other locale-dependent characters such as non-breaking space.
  • 如果您特别想检测空格而不是其他空白字符,也可以使用[:space:]

标签: r text stringr


【解决方案1】:

您可以在 \s 之前添加第二个 \,使其成为“\\s”:

data_frame$date_plus_chiffre=str_extract(string=data_frame$com,
                                         pattern="(chiffres)\\s(au)\\s([0-9]{2})(/)([0-9]{2})(/)([0-9]{4})")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2020-05-31
    • 2011-04-04
    • 2014-05-13
    • 1970-01-01
    • 2022-11-11
    • 2021-10-24
    相关资源
    最近更新 更多