使用gsub/regex在字符串内的名称周围加上引号 [英] Using gsub/regex to place quotes around names inside string

查看:70
本文介绍了使用gsub/regex在字符串内的名称周围加上引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索,但是找不到确切的内容.如果我忽略了我的话,我深表歉意.我正在尝试采用长字符串向量,它们都具有相同的一般结构,并将它们放入data.frame中.结构如下:

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

我最初的想法是使用gsub和一个正则表达式将/放在团队名称(例如/" Long Team Name/)周围,然后使用read.table进行导入,但是我遇到了一个困难regex表达式可以做到这一点,这将允许我以制表符分隔的字符串形式读取字符串,对吗?

谢谢! 布莱恩

解决方案

我认为这就是您想要的?

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中.

I have tried searching, but cannot find the exact thing I am looking to do. My apologies if I have overlooked it. I am trying to take a long vector of character strings, all with the same general structure, and place them into a data.frame. The structure is as follows:

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

My initial thought was to use gsub and a regex expression to place /" around the team names (ex. /"Long Team Name/") then use read.table to import, but I am running into difficulty coming up with the regex expression to do this. This would allow me to read in the string as tab delimited string, correct? If there is an easier suggestion, I am all ears.

Thanks! Brian

解决方案

I think this is what you want?

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"  

The result is a matrix, but you could wrap it in as.data.frame.

这篇关于使用gsub/regex在字符串内的名称周围加上引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆