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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
$ irb>> "module$block.item"[/\$(.*?)\./, 1]=> "block" Cheers, Andy -- Andy Jeffries http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS Company number: 5452840 On 23 March 2010 14:03, Marcio Machado <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
$ irb>> "module$block.item"[/\$([^.]*)\./,1]=> "block" -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Jeffries http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS Company number: 5452840 On 24 March 2010 07:15, DmitryPush <dmitrypush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> $ irb > > >> "module$block.item"[/\$([^.]*)\./,1] > > => "block" > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? On Mar 24, 12:00 pm, Andy Jeffries <andyjeffr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Jeffrieshttp://andyjeffries.co.uk/ #rubyonrails #mysql #jquery > Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS > Company number: 5452840 > > On 24 March 2010 07:15, DmitryPush <dmitryp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > $ irb > > > >> "module$block.item"[/\$([^.]*)\./,1] > > > => "block" > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 24 March 2010 09:22, DmitryPush <dmitrypush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Mar 31, 2:56 pm, tonypm <tonypmar...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.