1

I have a list of strings that end with a decimal followed by 3 digits, eg '07:02.334'. I need to extract those last three digits and thought that some form of split would work but everything I try fails, for instance:

> s <- '07:02.334'
> s
> str_split(s, '.')
[[1]]
 [1] "" "" "" "" "" "" "" "" "" ""

I've tried embedding the string s in a list, escaping the period "." and using whatever string splitters I've found while searching but none work.

I can use the below but I will need to do a split on other data later.

> file_ext(s)
[1] "334"
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Nate Lockwood
  • 3,325
  • 6
  • 28
  • 34

1 Answers1

1

We need to escape (\\.) or use fixed = TRUE as . is a metacharacter in regex and it can match any character

strsplit(s, '.', fixed = TRUE)[[1]][2]
[1] "334"

According to ?strsplit

split - character vector (or object which can be coerced to such) containing regular expression(s) (unless fixed = TRUE) to use for splitting. If empty matches occur, in particular if split has length 0, x is split into single characters. If split has length greater than 1, it is re-cycled along x.

Also, as strsplit, returns a list, extract the list with [[ and get the second element ([2])


Or wrap with fixed in str_split

library(stringr)
str_split(s, fixed('.'))[[1]][2]
[1] "334"

We can also get the output with trimws

trimws(s, whitespace = ".*\\.")
[1] "334"

Or with sub

sub(".*\\.", "", s)
[1] "334"
akrun
  • 874,273
  • 37
  • 540
  • 662