regular expression

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?

$ irb

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

=> “block”

Cheers,

Andy

$ irb

"module$block.item"[/\$([^.]*)\./,1]

=> "block"

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 :slight_smile:

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

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?

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 :slight_smile:

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

Interesting to see two regexperts slogging it out :slight_smile:

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

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

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