Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
/^a-eg-h/.match(';f';)在Ruby中给出null_Ruby_Regex - Fatal编程技术网

/^a-eg-h/.match(';f';)在Ruby中给出null

/^a-eg-h/.match(';f';)在Ruby中给出null,ruby,regex,Ruby,Regex,表达式/^a-eg-h/.match('f')在Ruby解释器(2.3.1)中给出了nil。我不知道出了什么问题 有什么建议吗 Ruby documentations声明“上面的表达式应该返回#”。正如在注释中提到的,您的模式是不正确的。看起来您正试图使用,但忽略了包含周围的方括号。您的模式目前只在以文本a-eg-h开头的字符串上匹配。您想要的模式是: /[^a-eg-h]/ 此外,尝试将字符串j与此模式匹配将失败,并在Ruby中返回nil,因为字符串与模式不匹配。更好的方法是: match

表达式
/^a-eg-h/.match('f')
在Ruby解释器(2.3.1)中给出了
nil
。我不知道出了什么问题

有什么建议吗


Ruby documentations声明“上面的表达式应该返回
#
”。

正如在注释中提到的,您的模式是不正确的。看起来您正试图使用,但忽略了包含周围的方括号。您的模式目前只在以文本
a-eg-h
开头的字符串上匹配。您想要的模式是:

/[^a-eg-h]/
此外,尝试将字符串
j
与此模式匹配将失败,并在Ruby中返回
nil
,因为字符串与模式不匹配。更好的方法是:

match = /[^a-eg-h]/.match(str)
if (match)
    do_something()
end

这些文件在哪里提出索赔?您的正则表达式将只匹配以
a-eg-h
开头的字符串。您最有可能查找在
[]
中定义的范围。您的意思是说
/[^a-eg-h]/.match('f')
,而不是
/^a-eg-h/.match('f')
“尝试将字符串f与此模式匹配将失败”-您的意思是什么<代码>/[^a-eg-h]/.match('f')工作正常。