0

I'm trying to create a RE for an exact match of a string that starts with at most two alphabetic characters and is followed by at most two numeric characters.

test <- function(x) {
valid_pattern <- "^[a-zA-Z]{,2}\\d{,2}$"
return(grepl(valid_pattern, x))
}
sapply(c("OX4", "sw33", "WJx9", "W392"), test)

I would like the above to return TRUE, TRUE, FALSE, FALSE. Currently, it returns TRUE for all. What am I missing?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Is `sw` valid? The `{,2}` is a [known issue](https://stackoverflow.com/a/47004617/3832970), it allows up to 3 chars. Is `valid_pattern <- "^[a-zA-Z]{0,2}\\d{0,2}$"` what you need? – Wiktor Stribiżew Nov 07 '22 at 17:47

0 Answers0