Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String between first two (.dots)

Tags:

r

Hi have data which contains two or more dots. My requirement is to get string from first to second dot. E.g string <- "abcd.vdgd.dhdsg"

Result expected =vdgd

I have used

pt <-strapply(string, "\\.(.*)\\.", simplify =  TRUE)

which is giving correct data but for string having more than two dots its not working as expected. e.g string <- "abcd.vdgd.dhdsg.jsgs" its giving dhdsg.jsgs but expected is vdgd

Could anyone help me.

Thanks & Regards,

like image 1000
Milan6687 Avatar asked Dec 18 '22 22:12

Milan6687


2 Answers

In base R we can use strsplit

ss <- "abcd.vdgd.dhdsg"
unlist(strsplit(ss, "\\."))[2]
#[1] "vdgd"

Or using gregexpr with regmatches

unlist(regmatches(ss, gregexpr("[^\\.]+", ss)))[2]
#[1] "vdgd"

Or using gsub (thanks @TCZhang)

gsub("^.+?\\.(.+?)\\..*$", "\\1", ss)
#[1] "vdgd"
like image 89
Maurits Evers Avatar answered Jan 02 '23 01:01

Maurits Evers


Another option:

string <- "abcd.vdgd.dhdsg.jsgs"

library(stringr)
str_extract(string = string, pattern = "(?<=\\.).*?(?=\\.)")
[1] "vdgd"

I like this one because the str_extract function will return the first instance of the correct pattern, but you could also use str_extract_all to get all instances.

str_extract_all(string = string, pattern = "(?<=\\.).*?(?=\\.)")
[[1]]
[1] "vdgd"  "dhdsg"

From here, you could index to get any position between two dots you want.

like image 31
AndS. Avatar answered Jan 02 '23 01:01

AndS.