Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 正则表达式:只能接受字符串,其他什么都不能接受?_Ruby On Rails_Ruby_Regex_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 正则表达式:只能接受字符串,其他什么都不能接受?

Ruby on rails 正则表达式:只能接受字符串,其他什么都不能接受?,ruby-on-rails,ruby,regex,ruby-on-rails-4,Ruby On Rails,Ruby,Regex,Ruby On Rails 4,我正在开发rubyonrails以下是regex string_regex = /[a-z]+\z/i validates :name , :format => { :with => string_regex , :message => "should not contain special character" } 尝试: dbcda->true abjdbcak->true jgh1213->false 1314134@#$->错误 jbh31$->false ,,,,,

我正在开发
rubyonrails
以下是
regex

string_regex = /[a-z]+\z/i
validates :name , :format => { :with => string_regex , :message => "should not contain special character" }
尝试

  • dbcda->true
  • abjdbcak->true
  • jgh1213->false
  • 1314134@#$->错误
  • jbh31$->false
  • ,,,,,
    ->
    false
  • kjgh,g,,
    ->
    TRUE
    (应该是false)

  • 您需要使用
    /\A[A-z]+\z/i
    ,因为从字符串的开头(^)到结尾(\z)不需要任何特殊字符

    要寻求帮助,请尝试


    [Edit]如@2called chaos所述发生了更改

    考虑使用开头/结尾元字符。我不认为
    kjgh,g,,
    可能是真的,因为存在
    \z
    锚(字符串的绝对结尾)。实际上,要想真正正确,需要使用
    \a
    \z
    进行定界<代码>^是行的开头,
    \A
    是字符串的开头。嗯,你说得对。。。或者它应该是/^[a-z]+$/i。。。整个生产线。。。好的,编辑。谢谢