R regex to validate user input is correct
I'm trying to practice writing better code, so I wanted to validate my input sequence with regex to make sure that the first thing I get is 开发者_高级运维a single letter A to H only, and the second is a number 1 to 12 only. I'm new to regex and not sure what the expression should look like. I'm also not sure what type of error R would throw if this is invalidated?
In Perl it would be something like this I think: =~ m/([A-M]?))/)
Here is what I have so far for R:
input_string = "A1"
first_well_row = unlist(strsplit(input_string, ""))[1] # get the letter out
first_well_col = unlist(strsplit(input_string, ""))[2] # get the number out
In R code, using David's regex: [edited to reflect Marek's suggestion]
validate.input <- function(x){
match <- grepl("^[A-Ha-h]([0-9]|(1[0-2]))$",x,perl=TRUE)
## as Marek points out, instead of testing the length of the vector
## returned by grep() (which will return the index of the match and integer(0)
## if there are no matches), we can use grepl()
if(!match) stop("invalid input")
list(well_row=substr(x,1,1), well_col=as.integer(substr(x,2,nchar(x))))
}
This simply produces an error. If you want finer control over error handling, look up the documentation for tryCatch
, here's a primitive usage example (instead of getting an error as before we'll return NA):
validate.and.catch.error <- function(x){
tryCatch(validate.input(x), error=function(e) NA)
}
Finally, note that you can use substr
to extract your letters and numbers instead of doing strsplit.
You asked specifically for "A through H, then 0-9 or 10-12". Call the exception "InvalidInputException" or any similarly named object- "Not Valid" "Input" "Exception"
/^[A-H]([0-9]|(1[0-2]))$/
In Pseudocode:
validateData(String data)
if not data.match("/^[A-H]([0-9]|(1[0-2]))$/")
throw InvalidInputException
精彩评论