if ($foo =~ m/(?:.{0,40}\n){0,4}/m) { print "ok" }
/m is for "multi-line" match
update: see bart below for why is this wrong . See sauoq answer instead.
update: now I've got it (I did learn something today!) :
The RE is: /((?:[^\n]{0,40}\n){0,4})/
or: /((?:.{0,40}\n){0,4})/ # thanks sauoq!
roughly meaning: group( (up-to-40 non-line-breaks, line-break) up-to-4 times)close-group
Test code:
$line = "x" x 38 . "\n"; sub test { ($res) = ($_[0] =~ /((?:[^\n]{0,40}\n){0,4})/); if ($res) { print "yes\n"; } else { print "no\n"; } } test( "x$line" x 4 ); # 39 x 4 test( "xx$line" x 4 ); # 40 x 4 test( "xxx$line" x 4 ); # 41 x 4 test( "xx$line" x 3 ); # 40 x 3 test( "x$line" x 5 ); # 39 x 5
output:
yes yes no yes yes
Thanks thelenm and BrowserUk. sauoq got something very similar too.
sauoq noted that [^\n] is the same as a "dot".
In reply to Re: How do I match lines of 40 characters long in a block of text?
by fglock
in thread How do I match lines of 40 characters long in a block of text?
by kleinbiker7
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |