but there are some other possibilities using lookahead:/^([01])[01]*$/
/^(?=(.))[01]+$/
/^(?=[01]+$)(.)/
I feel a benchmark coming up...
Result:use Benchmark 'cmpthese'; cmpthese(-3, { plain => sub { my $x; ($x) = /^([01])[01]*$/ foreach qw(0 1 00 11 1 +0 01 000x00) }, capture_in_lookahead => sub { my $x; ($x) = /^(?=(.))[01]+$/ foreac +h qw(0 1 00 11 10 01 000x00) }, lookahead_then_capture => sub { my $x; ($x) = /^(?=[01]+$)(.)/ fore +ach qw(0 1 00 11 10 01 000x00) }, });
Well... "plain" appears to be slightly faster, but I wouldn't bother worrying about the difference. Whjat's a few percent, anyway?Benchmark: running capture_in_lookahead, lookahead_then_capture, plain +, each for at least 3 CPU seconds... capture_in_lookahead: 3 wallclock secs ( 3.41 usr + 0.00 sys = 3.41 + CPU) @ 24781.52/s (n=84505) lookahead_then_capture: 4 wallclock secs ( 3.13 usr + 0.00 sys = 3. +13 CPU) @ 24592.33/s (n=76974) plain: 3 wallclock secs ( 3.08 usr + 0.00 sys = 3.08 CPU) @ 24 +988.64/s (n=76965) Rate lookahead_then_capture capture_in_looka +head plain lookahead_then_capture 24592/s -- + -1% -2% capture_in_lookahead 24782/s 1% + -- -1% plain 24989/s 2% + 1% --
In reply to Re: Regex failure interpretation
by bart
in thread Regex failure interpretation
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |