简体   繁体   English

Ruby Regex验证格式xxx-####

[英]Ruby Regex to validate format xxx-####

I need to validate input evidence number as per following formats in my model. 我需要按照模型中的以下格式验证输入证据编号。

XXX-### 
XXX-#### 
XXX-##### 
XXX-######
XXXX-#####

XXXX are alphabets and #### are numeric digits XXXX是字母,####是数字

I have following in my model code. 我的模型代码中有以下内容。

validates_format_of :evidence_number, :with=> /[A-Z a-z]{3}-\d{3,6}/

It only works for digits less than 3 ie it works for XXX-12 but does not work for digits more than 6 ie it does not work for XXX-1234567 它仅适用于小于3的数字,即适用于XXX-12,但不适用于大于6的数字,即不适用于XXX-1234567

You should be using the \\A (beginning of string) and \\z (end of string) anchors, not ^ (beginning of line) and $ (end of line): 您应该使用\\A (字符串的开头)和\\z (字符串的结尾)锚,而不是^ (行的开头)和$ (行的结尾):

validates_format_of :evidence_number, :with=> /\A[A-Z a-z]{3}-\d{3,6}\z/

A simple example will illustrate the difference: 一个简单的例子将说明差异:

>> "wrong\nXXX-999\nanchors" =~ /^[A-Z a-z]{3}-\d{3,6}$/
=> 6
>> "wrong\nXXX-999\nanchors" =~ /\A[A-Z a-z]{3}-\d{3,6}\z/
=> nil
>> "XXX-999" =~ /^[A-Z a-z]{3}-\d{3,6}$/
=> 0
>> "XXX-999" =~ /\A[A-Z a-z]{3}-\d{3,6}\z/
=> 0

You almost always want to use \\A and \\z instead of ^ and $ in Ruby. 您几乎总是想用\\A\\z代替Ruby中的^$

If you need "at least three digits" after the hyphen then you want: 如果您需要连字符后的“至少三位数”,那么您需要:

/\A[A-z a-z]{3}-\d{3,}\z/

And Kenny Grant is right, that space in the character class looks odd. 肯尼·格兰特Kenny Grant)是对的,角色类中的空格看起来很奇怪。

Try adding the ^ starts and $ ends with. 尝试添加^开头和$结尾。

validates_format_of :evidence_number, :with=> /^[A-Za-z]{3,4}-\d{3,6}$/

Use mu is too short 's answer though. 虽然使用mu is too short的答案。

You might be best to set up some test code, and test the regex separately (or ideally set up a unit test and test it that way). 您可能最好设置一些测试代码,并分别测试regex(或者理想情况下设置单元测试并以这种方式进行测试)。

invalid   = %w[777-2345 ABCD-12 ABCD-12345678 AB-12345678 ABC-1234567]
valid     = %w[ABC-12345 ABCD-123 ABCD-1234 ABCD-12345 ABCD-123456]

def validate codes,regex
  codes.each do |code|
  if code =~ regex
     puts "Valid code #{code}" 
   else
     puts "Invalid code #{code}"
   end
 end
end

When testing the above with your regex, some problems emerge. 在使用正则表达式测试以上内容时,会出现一些问题。 From your description, I don't understand why your regex includes a space as an allowed character - for codes this seems unlikely to be a valid code. 根据您的描述,我不明白为什么您的正则表达式包含空格作为允许的字符-对于似乎不太可能是有效代码的代码。 So you might be better with something like this: 因此,使用以下内容可能会更好:

/\A[A-Za-z]{3,4}-\d{3,6}\z/

Which limits the match to a full string, and limits the first match to 3 or 4 alpha characters, and the final match to 3-6 numbers. 这将匹配限制为一个完整的字符串,并将第一个匹配限制为3个或4个字母字符,最后一个匹配限制为3-6个数字。 This is assuming the #### is not a mistake in your examples: 这是在您的示例中假设####不是错误:

puts "\n\nUSING NEW REGEX"
puts "Validating valid codes"
validate valid,/\A[A-Za-z]{3,4}-\d{3,6}\z/
puts "\nValidating INVALID codes"
validate invalid,/\A[A-Za-z]{3,4}-\d{3,6}\z/

Add beginning- and end-of-string regex chars: 添加字符串开头和结尾的正则表达式字符:

validates_format_of :evidence_number, :with=> /^[A-Z a-z]{3}-\d{3,6}$/

ruby-1.9.3-p125 :000 > "ABC-1234567".match /[A-Z a-z]{3}-\d{3,6}/
 => #<MatchData "ABC-123456"> 
ruby-1.9.3-p125 :001 > "ABC-1234567".match /^[A-Z a-z]{3}-\d{3,6}$/
 => nil 

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

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