R: strsplit to backslash (\)

I am trying to extract part of a line before the first backslash, but I cannot get it to work fine.

I tried several ways to make it work based on the manual page for strsplit and after searching the web.

In my real situation, the rows are in the data frame that I get from the 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 result will also be divided by \ between BLAAT1 and 022E:

Thank you in advance

+5
source share
1 answer

Here is a regular expression that you can use: it breaks into \ and a non-printable character :

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

See IDEONE Demo

+6
source

Source: https://habr.com/ru/post/1234011/


All Articles