首页 > 解决方案 > R Regex,获取引号之间的字符串

问题描述

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

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

标签: pythonrregex

解决方案


两个想法:

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

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(.., "-")可以获取您想要的子字符串。


推荐阅读