Ruby on rails 都柏林邮政编码的正则表达式
我需要为都柏林邮政编码构建一个正则表达式查询,以便在Rails应用程序中验证表单 都柏林邮政编码范围为1-22,用户输入的信息包括: “都柏林22” “都柏林22” “都柏林22” “都柏林22” “D2” “d2” “D 2” “d 2” “2” 在Ruby中,我已经开始编写长验证程序方法来涵盖其中一些方法。我想知道在正则表达式中是否有更简单的解决方案Ruby on rails 都柏林邮政编码的正则表达式,ruby-on-rails,ruby,regex,Ruby On Rails,Ruby,Regex,我需要为都柏林邮政编码构建一个正则表达式查询,以便在Rails应用程序中验证表单 都柏林邮政编码范围为1-22,用户输入的信息包括: “都柏林22” “都柏林22” “都柏林22” “都柏林22” “D2” “d2” “D 2” “d 2” “2” 在Ruby中,我已经开始编写长验证程序方法来涵盖其中一些方法。我想知道在正则表达式中是否有更简单的解决方案 def formatted_postal_code # used to produce postal code in the fo
def formatted_postal_code
# used to produce postal code in the format "Dublin 2"
if self.postal_code_is_i? && self.postal_code.to_i.between?(1, 22)
# check if postal code is an integer
self.postal_code = "Dublin " + self.postal_code
elsif
else
# capitalize postal code
self.postal_code.capitalize
end
end
def postal_code_is_i?
/\A[-+]?\d+\z/ === self.postal_code
end
任何帮助或建议都将不胜感激 试试这个:
test = "dublin 22"
p "true" if test =~ /^d?(ublin)?(\s)?22?$/i
如果你说“输入包括:”,你是在提供示例,还是在提供完整的可能性范围?你真的需要提供后者。“都柏林22号”?“都柏林02”?“D-22”?“都柏林22号”?