Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove text after second colon

I need to remove everything after the second colon. I have several date formats, that need to be cleaned using the same algorithm.

a <- "2016-12-31T18:31:34Z"
b <- "2016-12-31T18:31Z"

I have tried to match on the two column groups, but I cannot seem to find out how to remove the second match group.

sub("(:.*){2}", "", "2016-12-31T18:31:34Z")
like image 341
Esben Eickhardt Avatar asked Oct 26 '25 12:10

Esben Eickhardt


2 Answers

A regex you can use: (:[^:]+):.*

which you can check on: regex101 and use like

sub("(:[^:]+):.*", "\\1", "2016-12-31T18:31:34Z")
[1] "2016-12-31T18:31"
sub("(:[^:]+):.*", "\\1", "2016-12-31T18:31Z")
[1] "2016-12-31T18:31Z"
like image 163
Marc Lambrichs Avatar answered Oct 29 '25 03:10

Marc Lambrichs


Let say you have a vector:

date <- c("2016-12-31T18:31:34Z", "2016-12-31T18:31Z", "2017-12-31T18:31Z")

Then you could split it by ":" and take only first two elements dropping the rest:

out = sapply(date, function(x) paste(strsplit(x, ":")[[1]][1:2], collapse = ':'))
like image 29
Aleksandr Avatar answered Oct 29 '25 04:10

Aleksandr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!