Regex: matching for only one number in an integer R

I have an example list of numbers:

888*
8*
8.88*
88.88*
88888.888*
899900
8.89
0.08
80
89899
50
32
30.8
0.081
0.8
8.1

and I only want to match those that have only 8’s. I put an asterisk for the ones I only want and the others should be ignored.

I tried this but could only get partially what I wanted.

num <- c(888,
  8,
  8.88,
  88.88,
  88888.888,
  899900,
8.89,
0.08,
80,
89899,
50,
32,
30.8,
0.081,
0.8,
8.1)

grepl('^8+[^\\.]*[^0-7|9]*', num)

>Solution :

How about:

grep("^[8.]+$", num, value = TRUE)
# "888"       "8"         "8.88"      "88.88"     "88888.888"

Leave a Reply