Re: No way of looking for a regrexp match starting from a particular point in a string?



On 04.06.2007 13:28, Robert Dober wrote:
On 6/4/07, Trans <transfire@xxxxxxxxx> wrote:


On Jun 4, 6:19 am, "Robert Dober" <robert.do...@xxxxxxxxx> wrote:
> On 6/3/07, Kenneth McDonald <kenneth.m.mcdon...@xxxxxxxxxxxxx> wrote:> I'm probably just missing something obvious, but I haven't found a way
> > to match a regular expression against only part of a string, in
> > particular only past a certain point of a string, as a way of finding
> > successive matches. Of course, one could do a match against a string,
> > take the substring past that match and do a match against the substring,
> > and so on, to find all of the matches for the string, but that could be
> > very expensive for very large strings.
>
> > I'm aware of the String.scan method, but that doesn't work for me
> > because it doesn't return MatchData instances.
>
> > What I want is just something like regexp.match(string, n),
>
> Hmm apart of using #scan and #index with $~ as indicated, I do not
> think that there is a performance penalty if you do
>
> rg.match(string[n..-1])

How can that be? You have to create a whole new String.
Beating a dead man Tom? As mentioned I had a terrible slip to C in my
reasoning, no idea why :(
If that can be avoided in the internal implementation then adding an optional offset
index to #match is not an unreasonable idea.

Robert, actually string[n..-1] is cheaper than you might assume: I believe the new string shares the char buffer with the old string, so you basically just get a new String object with a different offset - the large bit (the char data) is not copied.

Kind regards

robert
.



Relevant Pages

  • Re: String.each
    ... On 03.03.2007 09:00, Robert Dober wrote: ... > each character of a string and then processes it using a block. ... Using a String as array of lines does have it's uses at times but I wonder whether changing #each to return characters would be more useful. ...
    (comp.lang.ruby)
  • Re: something I just found out, am sharing (:newbish)
    ... On Fri, 24 Aug 2007, Robert Dober wrote: ... construction of another String / Array. ... Performance difference is already enormous for moderately long strings: ... & consulting: Ruby Power and Light, ...
    (comp.lang.ruby)
  • Re: No way of looking for a regrexp match starting from a particular point in a string?
    ... On 04.06.2007 14:06, Robert Dober wrote: ... believe the new string shares the char buffer with the old string, ... a formal parameter, the data has to be copied:( ...
    (comp.lang.ruby)
  • Re: Bug in sprintf?
    ... Robert Dober wrote: ... all you are saying is true BUT ... I do not think we should blame C for something which really is not nice. ... IMHO the interpreter should raise an expression when a String is passed to ...
    (comp.lang.ruby)
  • Re: Reading contents of a file and storing
    ... Robert Dober wrote: ... of a string (even knowing it is a line)? ... you're expecting Perl-like regular expression behaviour. ...
    (comp.lang.ruby)

Loading