but there is a possibility that yours won't cater for: A string containing < 40 chars but no newline..

You're right. Mine doesn't account for it. I guess I assumed they all would end with newlines. That was a bad assumption on my part. Of course, I might blame it on poorly stated requirements. :-)

(Knowing you, you'll add a 4 character, positively backward, forward-looking, zero-width assertion to your regex and acheive that too:)

Nah... it should be easier than that. Use a $ to match the end of the line (not including the newline) and then \n? to match an optional newline. So, I tried that:

/ ( # Assuming you want capture these lines. (?: # Group each line. ^ # Beginning of the line. .{0,40}$\n? # 0 to 40 chars, an end-of line and optional newline +. ){1,4} # 1 to 4 lines. (0 will permit an empty match.) ) # Done capturing. /mx; # /m so that ^ anchor works, /x for comments.

But that didn't work! I was vexed until I realized that looks an awful lot like "match 0 to 40 characters followed by $\ followed by an optional "n". So, then I tried:

/ ( # Assuming you want capture these lines. (?: # Group each line. ^ # Beginning of the line. .{0,40}$ # 0 to 40 characters followed by an end-of-line. \n? # An optional newline. ){1,4} # 1 to 4 lines. (0 will permit an empty match.) ) # Done capturing. /mx; # /m so that ^ anchor works, /x for comments.

And that worked like a charm.

That additional requirement did make the whole exercise more fun. There is another workaround. Sometime before I actually figured out why it was breaking, I tried (?:\n|\Z) and that worked as well but I thought it was ugly. So, I'm left wondering whether there is a better way around it than using /x and whitespace.

Thanks for making this so much more entertaining. :-)

Update: This was my 300th node! :-)

-sauoq
"My two cents aren't worth a dime.";

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.