You need to enable JavaScript to run this app. R语言正则表达式:提取双引号之间的目标字符串
优惠活动
大模型
产品
解决方案
定价
更多

R语言正则表达式:提取双引号之间的目标字符串

R 提取包含指定文本的双引号内容

实现代码

加载stringr包的写法

# 定义待处理字符串
raw_str <- 'c:1:{s:7:"note";s:335:"Document is original-no need to register again";}'

library(stringr)
# 匹配目标内容
target_content <- str_match(raw_str, '"([^"]*Document is original[^"]*)"')[,2]
target_content

运行输出:

[1] "Document is original-no need to register again"

基础R无依赖写法

无需安装/加载第三方包,使用R内置函数实现同样效果:

raw_str <- 'c:1:{s:7:"note";s:335:"Document is original-no need to register again";}'
target_content <- regmatches(
  raw_str,
  regexec('"([^"]*Document is original[^"]*)"', raw_str)
)[[1]][2]
target_content

正则匹配规则说明

  • 限定Document is original必须处于一对双引号的内部,不会抓取字符串外的零散匹配
  • 用[^"]*限定匹配范围不超出当前双引号边界,不会跨多个引号错误拼接内容
  • 捕获组自动剔除前后的双引号符号,直接返回引号内部的纯文本内容
  • 不会误匹配前面的"note"字段,因为该字段引号内的文本不包含指定目标内容

内容的提问来源于stack exchange,提问作者Aglet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.09 17:15:04