[Solved] Extract with Specific Prefix and control no. of digits in Regex- R


You could try the below code which uses word boundary \b. Word boundary is used to match between a word character and a non-word character.

> library(stringr)
> str_extract_all(x, perl('\\b(?:[35948]\\d{9}|TAM\\d{5}|E\\d{7}|A\\d{5})\\b'))
[[1]]
[1] "3234567890" "5234567890" "9234567890" "4234567890" "8234567890"
[6] "TAM12345"   "E1234567"   "A12345"

solved Extract with Specific Prefix and control no. of digits in Regex- R