[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: Chopping the beginning of a string elegantly
From: david () vallner ! net
Date: 2005-08-01 12:56:04
Message-ID: 1122900957.42ee1bddcca3a () mail ! atlantis ! sk
[Download RAW message or body]
Citát "francisrammeloo@hotmail.com" <francisrammeloo@hotmail.com>:
> I'm sorry but my first post was misleading.
>
> The string actually looks like this:
>
> " BeginCaption 0, 20, 66, .."
> (the whitespace can be a combination of tabs and spaces)
>
> So I actually have to cut out "Caption" word in the <<middle>> of the
> string.
>
> Brian's hint got me on the right track. The solution I finally found
> was:
>
> pane = line.sub(/\s*Begin(\w+).*/, '\1')
>
> Unless some expert can prove a still more tight solution I believe I
> have found the best way to cut out the "Caption" part of the string.
>
> Thank you all for your help.
>
> Best regards,
> Francis
>
>
>
Hmm. A wild guess (e.g. until there's a Windows installer for Ruby 1.9 w/
Oniguruma, or someone makes a SuSE compatible RPM for that) is that lookbehinds
in REs could reduce that to:
pane = line[/(?<=Begin)\w+/]
There's a 95% chance you don't need those groups to catch the initial whitespace
and consume the rest of the string, so:
pane = line.match(/Begin(\w+)/)[1]
also works and has a bit less noise. Weirdly though, Benchmark tells me it's
slightly slower. Guess you can't have your cake and eat it.
David
(ikkle newbie and all that)
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic