简体   繁体   English

Ruby Regex用于以逗号分隔的特定格式字母数字

[英]Ruby Regex for specific format alphanumerics separated by comma

I need to validate that the text input in my rails form has contains one or more alphanumerics with specific format separated by comma. 我需要验证Rails表单中的文本输入是否包含一个或多个字母数字,其特定格式用逗号分隔。

For example the alphanumeric should be either two letters + seven numbers + one letter + one number or two letters + 11 numbers + one letter + one number . 例如,字母数字应为two letters + seven numbers + one letter + one numbertwo 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) 我如何建立一个ruby正则表达式来检查是否(这些字母数字具有特定格式)和(如果它们用逗号分隔)

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 . 并且您将在捕获组1中使用特定格式的字母数字。

INPUT 输入

US6174724B1 , US20010002490A1 (This is a valid one) US6174724B1,US20010002490A1(这是有效的)

ruby , rails (This is an invalid one) ruby,rails(这是无效的)

OUTPUT 输出值

Match 1: US6174724B1 
Group 1: US6174724B1

Match 2:  , US20010002490A1 
Group 1: US20010002490A1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM