【问题标题】:R Regex, get string between quotations marksR Regex,获取引号之间的字符串
【发布时间】:2021-04-18 01:30:28
【问题描述】:

所以。我正在尝试从下面的字符串中提取Document is original

c:1:{s:7:"note";s:335:"Document is original-no need to register again";}

【问题讨论】:

  • 我在示例中没有看到任何“文档不是原始文件”。
  • 而“note”二字也在问号之间。正则表达式应该如何工作?
  • 我看到答案已经提交了。下次请分享您的代码,以便我们知道您尝试了什么。

标签: python r regex


【解决方案1】:

两个想法:

一点点工作,得到该结构的大部分组件:

string <- 'c:1:{s:7:"note";s:335:"Document is original-no need to register again";}'

strcapture("(.*):(.*):(.*)",
           strsplit(regmatches(string, gregexpr('(?<={)[^}]+(?=})', string, perl = TRUE))[[1]], ";")[[1]],
           proto = list(s="", len=1L, x=""))
#   s len                                                x
# 1 s   7                                           "note"
# 2 s 335 "Document is original-no need to register again"

一个更简单的方法,也许更硬编码:

regmatches(string, gregexpr('(?<=")([^;"]+)(?=")', string, perl = TRUE))[[1]]
# [1] "note"                                          
# [2] "Document is original-no need to register again"

从这里开始,您需要弄清楚如何关闭 "note",然后也许是 strsplit(.., "-") 以获得您想要的子字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2015-06-24
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多