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.";
|