This (finally) will do what you asked. It will test a string and determine if it contains 0 to 4 lines, and if it has 1 or more lines, that each of those lines is less than 40 characters.

It wont search a scalar that has more than 4 lines and determine if there are 4 consecutive lines of less than fourty characters which I think is what Zaxo's solution does, and could be what you want, but it ain't what you asked for:)

To put it another way, it will fail if the string has more than 4 lines or if any of the lines it contains are > 40 chars. Is that what you wanted? Should it be true if it contains no lines? Or rather, some chars with no newline?

Oh, and its a 'pure regex' solution. (For some definition of pure:)

#! perl -sw use strict; local $"='';#" my ($tmp1, $tmp2); for my $lines (0..5) { my $willmatch = ((qq(@{[0..9]}) x 4).$/)x $lines; my $half = int($lines/2); my $wontmatch = ((qq(@{[0..10]}) x 4).$/)x$half . 'X' . ((qq(@{[0. +.9]}) x 4).$/)x(1+$lines-$half); for ($willmatch, $wontmatch) { # if the number of lines inthe string is less than 4 if( ($tmp1=()=m/\n/mg) <=4 # and they are all < 40 chars and ( $tmp2=()=m/^(?:[^\n]{0,40}\n)/mg ) == $tmp1 ) { print "\nMatches! ($tmp1 lines with $tmp2 < 40 chars)\n"; } else { print "\nNo Match! ($tmp1 lines with only $tmp2 < 40 chars +)\n"; } print $_.$/; } } __END__

For the results of the test click to

C:\test>200697 Matches! (0 lines with 0 < 40 chars) No Match! (1 lines with only 0 < 40 chars) X0123456789012345678901234567890123456789 Matches! (1 lines with 1 < 40 chars) 0123456789012345678901234567890123456789 No Match! (2 lines with only 1 < 40 chars) X0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 Matches! (2 lines with 2 < 40 chars) 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 No Match! (3 lines with only 1 < 40 chars) 012345678910012345678910012345678910012345678910 X0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 Matches! (3 lines with 3 < 40 chars) 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 No Match! (4 lines with only 2 < 40 chars) 012345678910012345678910012345678910012345678910 X0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 Matches! (4 lines with 4 < 40 chars) 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 No Match! (5 lines with only 4 < 40 chars) 012345678910012345678910012345678910012345678910 012345678910012345678910012345678910012345678910 X0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 No Match! (5 lines with only 4 < 40 chars) 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 No Match! (6 lines with only 4 < 40 chars) 012345678910012345678910012345678910012345678910 012345678910012345678910012345678910012345678910 X0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 0123456789012345678901234567890123456789 C:\test>

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

In reply to Re: How do I match lines of 40 characters long in a block of text? by BrowserUk
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.