stringr extract full number from string

前端 未结 1 1204
迷失自我
迷失自我 2020-12-11 21:48

From ?stringr::str_extract, I tried

 library(stringr)
 str_extract(\"number 123\", \"\\\\d\")
 # [1] \"1\"

but I\'d like the f

1条回答
  •  [愿得一人]
    2020-12-11 22:42

    For this particular example, the following regular expression works:

    pat <- "(\\d)+"
    as.numeric(str_extract("number 123", pat))
    # [1] 123
    

    If you want to only pick out numbers that terminate the text string that they are in, add a $ to the end of the pattern above.

    0 讨论(0)
提交回复
热议问题