0

I am trying to split a string into multiple bits when a specific set of characters is found. In this example "abc."

Here is my code

test <- "aa\abc.def/def\abd.abd...abc.def"
result <- str_split(test,"abc\\.")

So here my expected output is

"aa\" "def/def\abd.abd..." "def"

The output I get is:

"aa\abc.def/def\abd.abd..." "def"  

So it does seem to work at the end but does not work on the first one. I think it might be because of the "" but that still seems kinda of weird to me that the function would not consider the potential presence of a ""

Any help is welcome on this.

LBes
  • 3,366
  • 1
  • 32
  • 66

1 Answers1

2

The issue you experience is with the string itself, not the split function. You should be using "\\" instead of "\" in the string.

test = "aa\\abc.def/def\\abd.abd...abc.def"

cat(test)
# aa\abc.def/def\abd.abd...abc.def

result = str_split(test,"abc\\.")
result 

# [[1]]
# [1] "aa\\" "def/def\\abd.abd..." "def" 
Andrey Shabalin
  • 4,389
  • 1
  • 19
  • 18
  • I have no control over the string itself so that's not really an option on my side. Upvote given though as it does answer part of the question. – LBes Aug 08 '21 at 08:29
  • Please elaborate. How do you receive the string into your code? – Andrey Shabalin Aug 08 '21 at 08:31
  • As you defined the string now, it does not contain backslashes `"\"`. The line `cat(test)` shows `aabc.def/defbd.abd...abc.def` – Andrey Shabalin Aug 08 '21 at 08:32
  • It is from a list of files that I cannot modify (and considering how long they are I don't think I should go ahead and replace every escape character in them anyways) – LBes Aug 08 '21 at 08:33
  • Well, your regex is looking for a backslash while you want to capture an escape character? That sounds like a rather different question - build a regex to capture an escape character. – Andrey Shabalin Aug 08 '21 at 08:35
  • in this case \a is one character. so it can not recognize '\abc.' by 'abc.' actually, they are totally different. if it is not the problem of encoding put \\ instead of \ in your string – SSDN Aug 08 '21 at 08:44