# Regular expression

**URL:** <https://www.ruby-forum.com/t/regular-expression/185609>\
**Category:** Rails\
**Created:** [March 23, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/regular-expression/185609 "2010-03-23T15:03:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 23, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/regular-expression/185609/1 "2010-03-23T15:03:34Z")

</div>

Hi,

IÂ´m trying to write a regular expression to get the substring “block”  
in:  
“module$block.item”

IÂ´ve reached the obvious /$.\*./ witch returns “$block.” substring.

How do I get directly the “block” substring?

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 23, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/regular-expression/185609/2 "2010-03-23T15:19:40Z")

</div>

$ irb

> > “module$block.item”[/$(.\*?)./, 1]  
> > =\> “block”

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 24, 2010, 8:16am UTC](https://www.ruby-forum.com/t/regular-expression/185609/3 "2010-03-24T08:16:22Z")

</div>

$ irb

> > “module$block.item”[/$([^.]\*)./,1]

=\> “block”

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 24, 2010, 10:22am UTC](https://www.ruby-forum.com/t/regular-expression/185609/4 "2010-03-24T10:22:49Z")

</div>

A came to ruby from perl and as I know my solution faster there.  
Ofcourse in this case it doesn’t matter. But understating how works  
your  
and mine solutions may be useful…  
I’ve seen your massage and post mine only for educational reasons.  
Plus mine look’s nicer, I just kidding:)

PS I don’t know why it can be that in 1.8.7 look behind solution can  
be faster, you do?

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 24, 2010, 10:02am UTC](https://www.ruby-forum.com/t/regular-expression/185609/5 "2010-03-24T10:02:19Z")

</div>

Dmitry posted pretty much the same solution as I did, but a slightly  
different regular expression. On Ruby 1.8.7 (your Ruby version may  
vary) my  
regex is _slightly_ faster. On Ruby 1.9.1 his is _slightly_ faster.  
The  
difference is 0.3s over 10,000,000 iterations, so either way it’s pretty  
instantaneous and won’t be the bottleneck in your app 🙂

Just thought I’d post this as Dmitry posted his solution that looked  
identical to mine at a cursory glance…

Dmitry what was your reasoning behind posting the same solution with a  
slightly different regex without an explanation as to why? Did you not  
see  
my message? Just curious…

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 31, 2010, 12:57pm UTC](https://www.ruby-forum.com/t/regular-expression/185609/6 "2010-03-31T12:57:10Z")

</div>

Interesting to see two regexperts slogging it out 🙂

Seriously though, I have to confess I have a real block when it comes  
to regexp. There seem to be so many variants of ways of doing  
things. This example is the sort of thing I have done in the past,  
but never so elegantly. Trouble is I am not entirely sure what is  
going on in this syntax. I wonder if one of the two bright guys here  
could help with a little explanation of for example:

a. the square brackets causing the match?  
b. the 1  
c. look ahead?  
d. why does the 1 return block without the $

I would really like to get my head around this stuff. If you know of  
a good tutorial/guide that would be helpful too. The problem I find  
is that so many of the guides go over the same basic stuff, I seem to  
read loads of things and never get to the real meaty bits, or the  
explanation of the meaty bits then goes over my head. I don’t mind  
admitting to being dumb!

Thanks  
Tonypm

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 31, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/regular-expression/185609/7 "2010-03-31T16:12:08Z")

</div>

This is very good tutorial (don’t be upset its for perl, in ruby  
regexp works pretty the same)  
[http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/pod/perlretut.html](http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/pod/perlretut.html)

a. - [] - causing group of symbols, for eg /[bcr]at/ matches 'bat,  
‘cat’, or ‘rat’  
b. 1 - it’s a first math ($1 in perl)  
c. look ahead : by default regexp is greed, it means /._/ will math as  
much as possible but it some time you don’t need it  
then you can use /._?/ instead. what’s why ‘minimal match’ sort of  
look ahead to not match more then you need to.  
I’m not sure I explain my self clear enough :)) you can find all  
answers in tutorial that i post here.  
d. because it’s ruby syntax

---

<div class="post-metadata">

**Author:** ![mmmjr1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@mmmjr1](https://www.ruby-forum.com/u/mmmjr1)\
**Post date:** [March 24, 2010, 10:44am UTC](https://www.ruby-forum.com/t/regular-expression/185609/8 "2010-03-24T10:44:28Z")

</div>

On 24 March 2010 09:22, DmitryPush [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A came to ruby from perl and as I know my solution faster there.  
> Ofcourse in this case it doesn’t matter. But understating how works  
> your and mine solutions may be useful…

Absolutely

> I’ve seen your massage and post mine only for educational reasons.  
> Plus mine look’s nicer, I just kidding:)

LOL 🙂

> PS I don’t know why it can be that in 1.8.7 look behind solution can  
> be faster, you do?

No idea… To be honest, I expected your version to be slightly  
faster…

As an aside, both versions completed their 10M iterations in about 15s  
on  
Ruby 1.9.1 and 21s on Ruby 1.8.7 - nice to know there’s such a  
performance  
boost between the two Ruby versions…

Cheers,

Andy
