in reply to How do I match lines of 40 characters long in a block of text?
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".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How do I match lines of 40 characters long in a block of text?
by BrowserUk (Patriarch) on Sep 25, 2002 at 19:08 UTC | |
by bart (Canon) on Sep 25, 2002 at 20:42 UTC | |
by BrowserUk (Patriarch) on Sep 25, 2002 at 21:26 UTC | |
by fglock (Vicar) on Sep 25, 2002 at 19:10 UTC | |
by BrowserUk (Patriarch) on Sep 25, 2002 at 19:33 UTC | |
|
Re: Re: How do I match lines of 40 characters long in a block of text?
by thelenm (Vicar) on Sep 25, 2002 at 19:20 UTC | |
|
Re: Re: How do I match lines of 40 characters long in a block of text?
by sauoq (Abbot) on Sep 25, 2002 at 20:13 UTC |