Search code examples
rregexstringrgrepl

StringsR - catch correct numbers


I am trying to perform string filtering based on R. I have multiple hierarchies and I need to group them together

I have prepared an example:


library(stringr)
library(tidyverse)

numbers <- tibble(LEVEL = c('0.1', '0.1.1', '0.1.2', '0.11', '0.12', '0.11.1', '0.12.1', '0.12.2'))



# Return also different values - first shall only contained: 0.1, 0.1.1, 0.1.2
numbers %>% 
  filter(grepl("^0.1.?", LEVEL))


# Second shall only contained: 0.11, 0.11.1
# Third shall only contained: 0.12, 0.12.1, 0.12.2

String pattern I have used in grepl is not enough.


Solution

  • The regex patterns can be formulated in a more concise way:

    numbers %>% 
      filter(grepl("^0\\.1$|^0\\.1\\.", LEVEL))   # 0.1, 0.1.1, 0.1.2
    numbers %>% 
      filter(grepl("^0\\.11$|^0\\.11\\.", LEVEL)) # 0.11, 0.11.1
    numbers %>% 
      filter(grepl("^0\\.12$|^0\\.12\\.", LEVEL)) # 0.12, 0.12.1, 0.12.2