正则表达式中的`\ .`和`i`是什么?

时间:2015-08-15 18:04:29

标签: ruby regex literals

有人可以告诉我以下ant -p Buildfile: /home/aaron/Projects/DataStore/build.xml Main targets: ... test Runs unit tests. Reports are saved in ${test.report.dir}/ ... - \.行中的i:with用于什么?

validates :image_url, allow_blank: true, format: {
    with:    %r{\.(gif|jpg|png)\Z}i,
    message: 'must be a URL for GIF, JPG or PNG file.'
}

2 个答案:

答案 0 :(得分:7)

在Ruby中,您可以使用%r {...}文字创建 Regexps

以下是metacharacters (, ), [, ], {, }, ., ?, +, *.出现在模式中时具有特定含义。为了匹配它们,他们必须反斜杠。

i用于case insensitive匹配。

答案 1 :(得分:7)

%r{}用于正则表达式。

\.正在寻找文字字符.。您需要\才能逃脱,因为仅使用.完全意味着其他内容(任何字符匹配)。

i用于不区分大小写的搜索。

基本上,您的正则表达式匹配任何以.gif.jpg.png结尾的内容。由于不区分大小写的搜索,这些也可能类似于.GiF