I needed to extract an ID across multiple formats and different datasets, where the possible patterns are:
ID-xx-yy, ID-xx, ID
For example:
00018523-01-02, 078-11, 789522314H
strsplit did the work:
strsplit("00018523-01-02","-")[[1]][1]
## [1] "00018523"
strsplit("078-11","-")[[1]][1]
## [1] "078"
strsplit("789522314H","-")[[1]][1]
## [1] "789522314H"