2

I'm wondering what regex pattern I could use to detect if every character in a string is numeric.

So, here's what I'm thinking that isn't working:

stringr::str_detect("311apple", "[0-9]")
#> [1] TRUE

That statement is TRUE because there exists a numeric character in that string, but I'm trying to find a pattern so that it's only TRUE if every character is numeric.

Any ideas? Thanks!

6
  • 4
    Use ^[0-9]+$. Commented Dec 19, 2018 at 17:40
  • FWIW I saw the answer that this is a "duplicate" of, but it doesn't look like the answers are applicable to R. Also, maybe I can re-think this as a "detect if there are any non-numeric characters" potentially. Commented Dec 19, 2018 at 17:41
  • Thanks for the response @WiktorStribiżew. Want to turn that into an answer? Commented Dec 19, 2018 at 17:43
  • There are a lot of patterns that are applicable in R, stackoverflow.com/a/9011537/3832970, stackoverflow.com/a/9011683/3832970 Commented Dec 19, 2018 at 17:46
  • 1
    Another good R solution - stackoverflow.com/a/43195631/3832970 Commented Dec 19, 2018 at 17:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.