Home > Back-end >  split string by dot colon
split string by dot colon

Time:04-10

is there any way to split string by ".:" ?

string = "DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1"
strsplit( x = string,split = ".:" ) # didn't work
strsplit( x = string,split = "\\.:" ) # didn't work
strsplit( x = string,split = "\\.\\:" ) # didn't work

CodePudding user response:

Here is a workaround:

With str_extract and the regex '(?<=:)[^.] ' lookaround we match substring ((?<=:)) that precedes before any character that are not a . with regex ([^.] )

With str_replace we make use of string2 and replace it by ""

library(dplyr)
library(stringr)

string2 <- str_extract(string, '(?<=:)[^.] ')
string1 <- str_replace(string, string2, '')

output:

> string2 <- str_extract(string, '(?<=:)[^.] ')
> string1 <- str_replace(string, string2, '')
> string1
[1] "DNS Servers . . . . . . . . . . . :"
> string2
[1] " fec0:0:0:ffff::1%1"

CodePudding user response:

Or in base R with strsplit

 strsplit(string, "(?<=:)\\s ", perl = TRUE)[[1]]
  • Related