使用 gsub/regex 在字符串中的名称周围放置引号

Using gsub/regex to place quotes around names inside string

我已经尝试搜索,但找不到我想要做的确切事情。如果我忽略了它,我深表歉意。我正在尝试获取一个长字符串向量,所有字符串都具有相同的一般结构,并将它们放入 data.frame 中。结构如下:

[1]   "rank, team, record"
[2]   "1 Team 22-4"
[3]   "2 Long Team Name 20-6"

我最初的想法是使用 gsub 和正则表达式将 /" 放在团队名称周围(例如 /"Long Team Name/")然后使用 read.table 导入,但我是 运行 难以想出正则表达式来执行此操作。这将允许我以制表符分隔的字符串形式读取字符串,对吗?如果有更简单的建议,我洗耳恭听。

谢谢! 布莱恩

我想这就是你想要的?

library(stringi)

x = c("rank, team, record", "1 Team 22-4", "2 Long Team Name 20-6")

res = stri_replace_first_fixed(x, " ", "|")
res = stri_replace_last_fixed(res, " ", "|")

res = stri_split_fixed(res, pattern = "|", simplify = T)
#      [,1]    [,2]             [,3]    
# [1,] "rank," "team,"          "record"
# [2,] "1"     "Team"           "22-4"  
# [3,] "2"     "Long Team Name" "20-6"  

结果是一个矩阵,但您可以将其包装在 as.data.frame