Reputation: 35
I need to find Regex that has to be implemented in R to detect the first six numbers in this test case and output TRUE/FALSE.
test_case = c('401 501', '401-501', '(401)501', '401501', '+401501', '4011501')
I've tried this function:
str_detect(test_case, regex('^[- + \\d]{6}'))
but I can't quite get the outputs to appear as so:
TRUE TRUE TRUE TRUE FALSE FALSE
Upvotes: 1
Views: 61
Reputation: 76402
This seems to do it:
grepl('^[-\\(\\) ]*[[:digit:]]{3}[-\\(\\) ]*[[:digit:]]{3}$', test_case)
#[1] TRUE TRUE TRUE TRUE FALSE FALSE
Upvotes: 0
Reputation: 388862
You can remove the additional characters that you want to allow in data and then check if the vector has exactly 6 digits.
grepl('^\\d{6}$', gsub('[() -]', '', test_case))
#[1] TRUE TRUE TRUE TRUE FALSE FALSE
Same logic implemented in stringr
:
library(stringr)
str_detect(str_remove_all(test_case, '[() -]'), '^\\d{6}$')
Upvotes: 1