Extract string among 3 patterns stringr

I want to extract the stream code in R from the left column string. They are 1 to 2 digits long just after the last hyphen. Hope you can help. Thank you.

library(tibble)

service <- tibble('services' = c('HIPREHAB-HIPREHAB-1-F0752',
'HIPCCM-DIABCM-28-F0421',
'HIPSCTS-WOUND-6-F0652'
))

service

service$stream <- c(1, 28, 6)

service

1 Like

This code has three ways to extract the one or two digits between the hyphens.

library(stringr)
library(dplyr)

service <- tibble('services' = c('HIPREHAB-HIPREHAB-1-F0752',
                                 'HIPCCM-DIABCM-28-F0421',
                                 'HIPSCTS-WOUND-6-F0652'
))

service <- service |> 
  mutate(
    #extract "hyphen digits hyphen" then extract the digits from that
    stream = str_extract(str_extract(services, "-\\d{1,2}-"),"\\d+"),
    #extract the first digits, won't work if the preceding text can have digits
    stream2 = str_extract(services, "\\d{1,2}"),
    #extract the digits from between hyphens
    stream3 = str_extract(services, "(?<=-)\\d{1,2}(?=-)"))

service
#> # A tibble: 3 x 4
#>   services                  stream stream2 stream3
#>   <chr>                     <chr>  <chr>   <chr>  
#> 1 HIPREHAB-HIPREHAB-1-F0752 1      1       1      
#> 2 HIPCCM-DIABCM-28-F0421    28     28      28     
#> 3 HIPSCTS-WOUND-6-F0652     6      6       6

Created on 2022-08-25 by the reprex package (v2.0.1)

1 Like

@FJCC thank you so much. i am waiting for this solution. stream3 is the best. appreciated.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.