Regular expressions in Ruby

A

Abder-rahman Ali

I'm new to Ruby, and just want to ask about how to read the following
statement from the "Agile Web Development With Rails, 3rd edition" book:

%r{\.(gif|jpg|png)$}i

I knew that %r is a way to write a regular expression.

But, what is the $, and the i?

Thanks.
 
L

Luis Parravicini

I'm new to Ruby, and just want to ask about how to read the following
statement from the "Agile Web Development With Rails, 3rd edition" book:

%r{\.(gif|jpg|png)$}i

I knew that %r is a way to write a regular expression.

But, what is the $, and the i?

$ matches the end of the string and i makes the search case
insensitve. This is not Ruby specific, perphaps you should read about
regular expressions. See http://www.regular-expressions.info/ruby.html

Bye
 
J

Jesús Gabriel y Galán

I'm new to Ruby, and just want to ask about how to read the following
statement from the "Agile Web Development With Rails, 3rd edition" book:

%r{\.(gif|jpg|png)$}i

I knew that %r is a way to write a regular expression.

But, what is the $, and the i?

I always have this link handy:

http://www.zenspider.com/Languages/Ruby/QuickRef.html#11

$ matches the end of a line or string
i is a modifier that makes the regexp case insensitive.

Jesus.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jun 14, 2010 at 10:24 AM, Abder-rahman Ali <
I'm new to Ruby, and just want to ask about how to read the following
statement from the "Agile Web Development With Rails, 3rd edition" book:

%r{\.(gif|jpg|png)$}i

It matches a dot, followed by either gif or jpg or png, at the end of the
string. It does not care about case.

For example, it would match

foo.gif
foo.GiF
fOoO.jpg
bar.PNG


It would not match

foogif
foo.gif.
foo.giff
foo.jp
foo.pjg
foo.jpx
foo.jpg.csv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top