R/postcodes_validation.R

valid_postcode_1 <- function(x) {
  # remove whitespace
  x <- gsub("\\s", "", x)

  # convert to upper case
  x <- toupper(x)

  # regex (this allows for cas)
  pattern <- "^([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$"

  # return indices of postcodes that match pattern
  grep(pattern, x, perl = TRUE)
}



valid_postcode_1 <- function(x) {
  # remove whitespace
  x <- gsub("\\s", "", x)

  # convert to upper case
  x <- toupper(x)

  # regex (this allows for cas)
  pattern <- ^(([A-Z][0-9]{1,2})|(([A-Z][A-HJ-Y][0-9]{1,2})|(([A-Z][0-9][A-Z])|([A-Z][A-HJ-Y][0-9]?[A-Z])))) [0-9][A-Z]{2}$

  # return indices of postcodes that match pattern
  grep(pattern, x, perl = TRUE)
}
tjtnew/usefulR documentation built on May 11, 2019, 8:28 p.m.