简体   繁体   English

剥离R中的特殊字符

[英]Stripping special characters in R

I have the following string 我有以下字符串

> str    
[1]  "[ { \"category\" : \"book\"} , { \"category\" : \"Movie\"} , { \"category\" : \"Brand\"}]"

I want to strip it to get the following vector 我想剥离它以获得以下向量

> a
[1] "book" "Movie" "Brand" 

My problem is how to handle the "" and \\ within the string to pass it to either grep or gsub in R. This is what I did and I get an error 我的问题是如何处理字符串中的“”和\\,以将其传递给R中的grepgsub 。这是我所做的,但出现错误

> grep("^\[ \{ \\"category\\" : \\"([a-zA-Z0-9/]+)\\".*",str)
Error: '\[' is an unrecognized escape in character string starting "^\["

Am I even on the right track? 我是否在正确的轨道上?

Maybe the rjson package will sort you out: 也许rjson包会把你整理出来:

> require(rjson)
> unlist(fromJSON(str))
  category category category 
    "book"  "Movie"  "Brand" 

ignoring the names ("category") that's what you want. 忽略您想要的名称(“类别”)。 Wrap in as.vector() to get rid of those. 包装as.vector()摆脱那些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM