# Regular expressions in Ruby

**URL:** <https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088>\
**Category:** Ruby\
**Created:** [June 14, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088 "2010-06-14T17:24:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 14, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/1 "2010-06-14T17:24:03Z")

</div>

I’m new to Ruby, and just want to ask about how to read the following  
statement from the “Agile Web D. 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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 14, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/2 "2010-06-14T17:48:47Z")

</div>

Abder-rahman Ali wrote:

> %r{.(gif|jpg|png)$}i
> 
> I knew that %r is a way to write a regular expression.
> 
> But, what is the $, and the i?

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UK](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UK)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 14, 2010, 6:48pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/3 "2010-06-14T18:48:19Z")

</div>

Brian C. wrote:

> Abder-rahman Ali wrote:
> 
> > %r{.(gif|jpg|png)$}i
> > 
> > I knew that %r is a way to write a regular expression.
> > 
> > But, what is the $, and the i?
> 
> [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UK)

Thanks Brian.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/4 "2010-06-17T16:22:30Z")

</div>

On Mon, Jun 14, 2010 at 5:24 PM, Abder-rahman Ali  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m new to Ruby, and just want to ask about how to read the following  
> statement from the “Agile Web D. 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:

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

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

Jesus.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/5 "2010-06-17T16:22:30Z")

</div>

On Mon, Jun 14, 2010 at 10:24 AM, Abder-rahman Ali \<  
removed\_email\_address@domain.invalid\> wrote:

> I’m new to Ruby, and just want to ask about how to read the following  
> statement from the “Agile Web D. 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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/6 "2010-06-17T16:22:34Z")

</div>

On Mon, Jun 14, 2010 at 12:24 PM, Abder-rahman Ali  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m new to Ruby, and just want to ask about how to read the following  
> statement from the “Agile Web D. 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 [Ruby Regexp Class - Regular Expressions in Ruby](http://www.regular-expressions.info/ruby.html)

Bye

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/7 "2010-06-17T16:22:38Z")

</div>

After referring to the regular expressions references provided, test  
them out at [rubular.com](http://rubular.com).

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/8 "2010-06-17T16:22:11Z")

</div>

> But, what is the $, and the i?  
> → [http://rubular.com/](http://rubular.com/) → Regex quick reference

The \ escapes the . Thus . matches the ‘.’ character.

Marc W.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/regular-expressions-in-ruby/190088/9 "2010-06-17T16:22:40Z")

</div>

I suppose this shall help

> **[Ruby - Regular Expressions](http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm)**
>
> Ruby Regular Expressions - A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.

## – Thanks and Regards Saurabh P. +91-9922071155 skype: sorab\_pune yahoo & gtalk: saurabh.purnaye msn: removed\_email\_address@domain.invalid

please don’t print this e-mail unless you really need to.

On Mon, Jun 14, 2010 at 8:54 PM, Abder-rahman Ali \<
