Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Regex fun。为什么这个正则表达式不匹配我的字符串?_Regex - Fatal编程技术网

Regex fun。为什么这个正则表达式不匹配我的字符串?

Regex fun。为什么这个正则表达式不匹配我的字符串?,regex,Regex,我正在调试carrierwave。很有趣。我认为问题可以归结为: carrierwave_direct内部有以下代码: if record.new_record? && record.send("has_#{attribute}_upload?") && record.key !~ record.send(attribute).key_regexp 密钥\u regexp代码如下: /\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#

我正在调试carrierwave。很有趣。我认为问题可以归结为:

carrierwave_direct内部有以下代码:

if record.new_record? && record.send("has_#{attribute}_upload?") && record.key !~ record.send(attribute).key_regexp
密钥\u regexp代码如下:

  /\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/
当我调试时,其计算结果如下:

record.send(attribute).key_regexp
/\Astories\/\/main_images\/[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)\z/
我的店长是:

故事/{model.id}/main_图像

我认为这里有一个空白/部分,因为model.id为零

record.key是这样的:

/stories/main_imagescache/20160319-1549-2202-5826/blueapronimage.jpg

为什么它目前不匹配

看起来正则表达式有一个额外的斜杠,我不确定这部分是什么:

[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)
是a-f、数字或-,然后是a/,然后是任意数量的字母或数字,然后是a….之间的任意数量的字母吗

这是什么?我

什么是捕获组

这是什么?-我

我认为这就是问题所在:

record.key !~ /\Astories/
true

它应该是假的。

看看这个,它解释了它:

[a-f\d\-]+            # Matches a-f, any digit, - (hyphen)
                      # ^ One or more times (+)
\/                    # Matches a /
.+                    # Matches any character one or more times
\.                    # Matches a . (dot)
(?i)                  # States that anything after this is case-insesitive
                      # ^ Inline Flag
(jpg|jpeg|gif|png)    # Selects file extension
                      # ^ (either jpg, jpeg, gif, or png)
(?-i)                 # Removes Inline Flag
这应该可以解释每个人都做了什么


也看看这个。您的示例似乎匹配,可能代码的另一部分不正确…

将正则表达式放入或。然后,您可以将鼠标悬停在部件上,它将准确地告诉您它的功能。它们都有一个解释部分,为您解释正则表达式。显然,这是错误的行为:record.key!~/\Astories/It returns True我把它放在了现场演示中,它似乎工作得很好,