I have a time data looks like this:
2024-01-26 19:24:40.0
I want to extract everything before the second colon like this: 2024-01-26 19:24
I tried the code as below:
a="2024-01-26 19:24:40.0"
b=sub("(^([^:]+:){2}).*$", "\\1",a)
But the result was like this: 2024-01-26 19:24:
So how to write the code in the right way?
>Solution :
Option 1
> sub("(.*\\d+:\\d+):.*", "\\1", a)
[1] "2024-01-26 19:24"
Option 2
> sub(":\\d+\\.\\d+", "", a)
[1] "2024-01-26 19:24"