Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading