Question: I'm trying to extract a number from a string.

And do something like `[0-9]+` on the string `"aaa12xxx"` and get `"12"`.

I thought it would be something like:

```
> grep("[0-9]+", "aaa12xxx", value=TRUE)
[1] "aaa12xxx"

```

And then I figured... 

```
> sub("[0-9]+", "\\1", "aaa12xxx")
[1] "aaaxxx"

```

But I got some form of response doing:

```
> sub("[0-9]+", "ARGH!", "aaa12xxx")
[1] "aaaARGH!xxx"

```

There's a small detail I'm missing.

Answer: 
For your specific case you could remove all not numbers:

```
gsub("[^0-9]", "", "aaa12xxxx")
# [1] "12"

```

It won't work in more complex cases

```
gsub("[^0-9]", "", "aaa12xxxx34")
# [1] "1234"

```
A solution for this question

```
library(stringr)
str_extract_all("aaa12xxx", regex("[[:digit:]]{1,}"))
# [[1]]
# [1] "12"

```

**[[:digit:]]**: digit [0-9]

**{1,}**: Matches at least 1 times