R: strsplit on backslash (\)

一曲冷凌霜 提交于 2019-12-21 16:22:42

问题


I am trying to extract the part of the string before the first backslash but I can't seem to get it tot work properly.

I have tried multiple ways of getting it to work, based on the manual page for strsplit and after searching online.

In my actual situation the strings are in a dataframe which I get from a database connection but I can simplify the situation with the following:

> strsplit("BLAAT1\022E:\\BLAAT2\\BLAAT3","\\",fixed=TRUE)
[[1]]
[1] "BLAAT1\022E:" "BLAAT2"      "BLAAT3"  

> strsplit("BLAAT1\022E:\\BLAAT2\\BLAAT3","\\",fixed=FALSE)
Error in strsplit("BLAAT1\022E:\\BLAAT2\\BLAAT3", "\\", fixed = FALSE) : 
  invalid regular expression '\', reason 'Trailing backslash'

> strsplit("BLAAT1\022E:\\BLAAT2\\BLAAT3","\\\\",fixed=TRUE)
[[1]]
[1] "BLAAT1\022E:\\BLAAT2\\BLAAT3"

> strsplit("BLAAT1\022E:\\BLAAT2\\BLAAT3","\\\\",fixed=FALSE)
[[1]]
[1] "BLAAT1\022E:" "BLAAT2"       "BLAAT3"      

The expected output would also split on the \ between BLAAT1 and 022E:

Thanks in advance


回答1:


Here is a regex that you can use: it splits at \ and a non-printable character:

strsplit("BLAAT1\022E:\\BLAAT2\\BLAAT3","[\\\\]|[^[:print:]]",fixed=FALSE)
# [1] "BLAAT1" "E:"     "BLAAT2" "BLAAT3"

See IDEONE demo



来源:https://stackoverflow.com/questions/33210280/r-strsplit-on-backslash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!