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>
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |