r-用句点+冒号分隔字符串



是否有任何方法可以通过"分割字符串:">

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

这里有一个解决方法:

使用str_extract和正则表达式'(?<=:)[^.]+'环视,我们将任何非.字符之前的子字符串((?<=:))与正则表达式([^.]+)匹配

str_replace中,我们使用了string2,并用""代替了它

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

输出:

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

或在base R中使用strsplit

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

最新更新