159

In a model there is a field

validates :image_file_name, :format => { :with => %r{\.(gif|jpg|jpeg|png)$}i

It looks pretty odd for me. I am aware that this is a regular expression. But I would like:

  • to know what exactly it means. Is %r{value} equal to /value/ ?
  • be able to replace it with normal Ruby regex operator /some regex/ or =~. Is this possible?

5 Answers 5

305

%r{} is equivalent to the /.../ notation, but allows you to have '/' in your regexp without having to escape them:

%r{/home/user}

is equivalent to:

/\/home\/user/

This is only a syntax commodity, for legibility.

Edit:

Note that you can use almost any non-alphabetic character pair instead of '{}'. These variants work just as well:

%r!/home/user!
%r'/home/user'
%r(/home/user)

Edit 2:

Note that the %r{}x variant ignores whitespace, making complex regexps more readable. Example from GitHub's Ruby style guide:

regexp = %r{
  start         # some text
  \s            # white space char
  (group)       # first group
  (?:alt1|alt2) # some alternation
  end
}x
4
  • 5
    The downvote was 1 hour ago only. As for space as delimiter, this is awesome, but not the sort of thing I will recommand to be able to read your code without beeing puzzled six month later :)
    – Eureka
    Sep 21, 2012 at 10:59
  • 3
    If you use spaces as your delimiter, your regular expression will break if you add spaces to it later. I think the idea is to use delimiters that don't match anything in your regular expression, with curly braces as the preferred default. May 18, 2015 at 18:42
  • 1
    It's almost as if ruby was designed to write as horrible code as possible
    – Roman
    Sep 13, 2018 at 22:27
  • Don't understand the note of your second edit. What is special about %r{}x? /.../x also ignores whitespace. This is no special property of %r syntax. All options that can follow /../ can also follow %r{}.
    – Mecki
    Mar 22, 2020 at 22:47
12

With %r, you could use any delimiters.

You could use %r{} or %r[] or %r!! etc.

The benefit of using other delimeters is that you don't need to escape the / used in normal regex literal.

0
10

\. => contains a dot
(gif|jpg|jpeg|png) => then, either one of these extensions
$ => the end, nothing after it
i => case insensitive

And it's the same as writing /\.(gif|jpg|jpeg|png)$/i.

4
  • 1
    Is %r{value} equal to /value/ ?
    – Alexandre
    Sep 12, 2012 at 9:10
  • Yes, it's the same. - As %w[1 2 3] is the same as [1, 2, 3] for example. Sep 12, 2012 at 9:11
  • 9
    %w[1 2 3] is not the same as [1, 2, 3]. It is the same as ['1', '2', '3']. Sep 12, 2012 at 12:46
  • @JörgWMittag Thanks for the rectification. Sep 12, 2012 at 13:45
3

this regexp matches all strings that ends with .gif, .jpg...

you could replace it with

/\.(gif|jpg|jpeg|png)$/i
0
1

It mean that image_file_name must end ($) with dot and one of gif, jpg, jpeg or png.

Yes %r{} mean exactly the same as // but in %r{} you don't need to escape /.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.