tags:

views:

83

answers:

2

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  
+1  A: 

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
David Souther
+2  A: 

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.

Leo Alekseyev
To make the first line shorter and easier to understand, see `str_detect` in the `stringr` package.
hadley
Thanks for updating that. Having never programmed R, I was at a loss there. I presume it has Perl-type regex syntax?
David Souther
Yes, it does basic, extended (default), and PCRE (using perl=TRUE flag) expressions.@hadley: `stringr` looks very handy!
Leo Alekseyev
Instead of `length(grep())>0` one could use `grepl()`.
Marek