regex - Deriving Phone number from a string in R -


i have vectors below:

i converted characters, special characters x

xxxxxx18002514919xxxxxxxxxxxxxxxxxxxxxxxxxx24xxxxxx7  xxxxxx9000012345xxxxxxxxxxxxx34567xxxxxxxxxxxxx1800xxxxxx7 

how can derive 11 digit or 10 digit phone number above strings in r

my desired output is: first string: 18002514919 second string: 9000012345

you can use stringr solve problem, there function called str_extract_all extract phone number desired.

the regex: \\d --> represent number, {n,m} --> curly braces matching times of number. here n applied minimum no of matches , m maximum number of numbers match. since want match phone number length between 10 , 11. n becomes 10 , m becomes 11.

x <- c("xxxxxx18002514919xxxxxxxxxxxxxxxxxxxxxxxxxx24xxxxxx7","xxxxxx9000012345xxxxxxxxxxxxx34567xxxxxxxxxxxxx1800xxxxxx7") library(stringr) str_extract_all(x,"\\d{10,11}") 

answer:

> str_extract_all(x,"\\d{10,11}") [[1]] [1] "18002514919"  [[2]] [1] "9000012345" 

if sure 1 scalar contain 1 string of phone number use str_extract.

> str_extract(x,"\\d{10,11}") [1] "18002514919" "9000012345"  

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -