library(stringr)text = "123-4567-C-4321-DD.xxxx"pattern = "(?i)(?<=-[CP]-)[0-9]{4}"print(str_match(text, pattern)[,1])
Standard input is empty
[1] "4321"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!