The Ruby %r{ } expression

The Ruby %r{ } expression

Add Comment


  • 3 Answer(s)

    Using %r{} is equivalent to the /…/ notation, but allows you to have ‘/’ in regexp without having to escape:

    %r{/home/user}
    

    equivalent to:

    /\/home\/user/
    

    Almost any non-alphabetic character pair instead of ‘{}’:

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

    Answered on January 11, 2019.
    Add Comment

    Follow this expressions:

    \. => 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.

    Answered on January 11, 2019.
    Add Comment

      Using %r, could use any delimiters  %r{} or %r[] or %r!! etc.that  do not need to escape the / used in normal regex literal.

    Answered on January 11, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.