Why regexp? IPv4 loop

is_valid_ipv4_loop <- function(ip) {
  octets <- strsplit(ip, "\\.")[[1]]

  if (length(octets) != 4) {
    # print("number of octets is not equal to 4")
    return(FALSE)
  }

  for (octet in octets) {
    if (!all(as.numeric(octet) >= 0, as.numeric(octet) <= 255)) {
      # print(as.numeric(octet))
      return(FALSE)
    }
  }

  TRUE
}

ip1 <- "192.168.1.1"
ip2 <- "256.0.0.1"
ip3 <- "192.168.001.001"

is_valid_ipv4_loop(ip1)
## [1] TRUE
is_valid_ipv4_loop(ip2)
## [1] FALSE
is_valid_ipv4_loop(ip3)
## [1] TRUE