0

I need to validate that the text input in my rails form has contains one or more alphanumerics with specific format separated by comma.

For example the alphanumeric should be either two letters + seven numbers + one letter + one number or two letters + 11 numbers + one letter + one number . And these alphanumerics must be separated by comma (,)

US6174724B1 , US20010002490A1 (This is a valid one)

ruby , rails (This is an invalid one)

How can I build a ruby regular expression that checks if (these alphanumerics have specific format) AND if (they are separated by comma)

SteveSt
  • 1,277
  • 2
  • 16
  • 23

1 Answers1

3

You could use this:

PATTERN

/^(?:(?:^| , )([A-Z]{2}(?:\d{7}|\d{11})[A-Z]\d)\b)+?$/

And you will have your alphanumerics in specific format in capture group 1.

INPUT

US6174724B1 , US20010002490A1 (This is a valid one)

ruby , rails (This is an invalid one)

OUTPUT

Match 1: US6174724B1 
Group 1: US6174724B1

Match 2:  , US20010002490A1 
Group 1: US20010002490A1
Community
  • 1
  • 1
Tafari
  • 2,639
  • 4
  • 20
  • 28
  • 1
    It works :) . Two questions only , 1) I had to add a forward slash in the beginning and in the end of the regex because otherwise I had an error when I tried to load my page. Why is that ? 2. What do you mean that the alphanumerics will be available in capture group 1 ? – SteveSt Nov 29 '13 at 14:00
  • @Stefanos it depends on the language, usually you should add slash at the beginning and at the end of the pattern just as you did : ) My 'native' language is C# which doesn't require me to do this, anyway added them to the answer as you are using ruby : ) – Tafari Nov 29 '13 at 14:01
  • nice :) , and what do you mean that the alphanumerics will be available in capture group 1 ? – SteveSt Nov 29 '13 at 14:51
  • @Stefanos I'm not sure how about ruby but usually using regex you can **capture certain groups** within **one match**, well sounds bit complicated huh? The capture group is inside parenthesis **( )**, so it will be possible to access certain group via code without including unnecessary things. Maybe this link will explain it better and more briefly [www.regular-expressions.info](http://www.regular-expressions.info/brackets.html) also I have found this question in ruby about capture groups [link](http://stackoverflow.com/questions/9303984/ruby-regexp-group-matching-assign-variables-on-1-line) – Tafari Nov 29 '13 at 18:52
  • Ok , I think I understood how it works. Btw I noticed that the above regex thinks this as valid : US6174724B1invalid whereas it is not since it contains more characters in the end ... – SteveSt Nov 29 '13 at 19:58
  • @Stefanos yeah true, modified it by adding **\b** (word boundry) at the end : ) you can also use **\s** (whitespace) instead and it should work properly as well. I'm telling you this as **\b** sometimes has poor implementation in some languages. – Tafari Nov 29 '13 at 20:15
  • :well I think that there is one last thing missing and then it will be completely fine : If I have this one " US6174724B1invalid , US20010002490A1 " Then the regex says that this is correct although it is not because the first alphanumeric is not correct. Is it possible to capture this ?? – SteveSt Nov 29 '13 at 21:10
  • 1
    @Stefanos Ok added the string boundries at the beginning and at the end of the pattern, it should work as intented now. – Tafari Nov 29 '13 at 21:31