Finding string of numbers (like telephone numbers) in text

Hi,
I am trying to find telephone numbers in my data frame.
I would like to specify that if there is any comments containing at least 10 consecutive numbers it should be coded as TMC.Number.
I can only find records containing numbers :frowning:

 
library(tidyverse)

sample_data <- data.frame(stringsAsFactors=FALSE,
                          URN = c(94, 59, 100, 7),
                          all_comment = c("number 12587 is funny", "bla bla bla", "call 45785854844", "all 100%"))
sample_data


library(dplyr)
library(stringr)
coding.result <- sample_data %>%
  mutate(
         TMC.Number =  if_else(str_detect(all_comment, regex("1|2|3|4|5|6|7|8|9", ignore_case = TRUE, multiline = TRUE))
                               &!str_detect(all_comment, regex("100%", ignore_case = TRUE)),  1, 0) 
  ) 

coding.result

Can you help?

instead of this try

"[0-9]{10}"
1 Like

This is the regex for 10 or more consecutive digits \\d{10,}

2 Likes

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.

Hurray! That was simple. Thank you :grinning: