in reply to Re: How do I match lines of 40 characters long in a block of text?
in thread How do I match lines of 40 characters long in a block of text?

Won't that match 0 to 4 lines of (0 to 40 characters + nl)?

Update:Of course it wil, cos that's what he asked for! D'oh!


Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
  • Comment on Re: Re: How do I match lines of 40 characters long in a block of text?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: How do I match lines of 40 characters long in a block of text?
by bart (Canon) on Sep 25, 2002 at 20:42 UTC
    I'm still curious why he would want to match zero lines. I'd expect him to want to match at least one line.

    And: as soon as you ask for at least one line, without an "^" anchor, your first line might contain more than 40 characters, only the regex it will only grab the last 40 ones of that line!

    In short: it is definitely not a bad idea to add an anchor.

    /^((?:.{0,40}\n){0,4})/;
    or
    /^((?:.{0,40}\n){1,4})/m;
    The latter case can grab 1 to 4 whole lines anywhere in your text.

      Agreed bart, I'm not sure that the OP has clearly stated his req's or perhaps he doesn't have a clear picture of what his req's are.

      I agree with you about the need for anchors, but despite using either /m or /s, I couldn't get things to work with just that. I ended up using \A which worked better, but on its own didn't seem to be enough. I also played with using $, \Z and \z, but I admit I am still a little confused about quite what each does in the contexts of /s and /m. Something I need to play with more.


      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Re: Re: How do I match lines of 40 characters long in a block of text?
by fglock (Vicar) on Sep 25, 2002 at 19:10 UTC

    Sure!

    I want to match up to 4 lines of up to 40 characters of text each

      Though yours will match 4 lines of 41 chars (or 5 of 77 infact) as well.


      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!