/^([01])[01]*$/ #### /^(?=(.))[01]+$/ #### /^(?=[01]+$)(.)/ #### use Benchmark 'cmpthese'; cmpthese(-3, { plain => sub { my $x; ($x) = /^([01])[01]*$/ foreach qw(0 1 00 11 10 01 000x00) }, capture_in_lookahead => sub { my $x; ($x) = /^(?=(.))[01]+$/ foreach qw(0 1 00 11 10 01 000x00) }, lookahead_then_capture => sub { my $x; ($x) = /^(?=[01]+$)(.)/ foreach qw(0 1 00 11 10 01 000x00) }, }); #### 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) @ 24988.64/s (n=76965) Rate lookahead_then_capture capture_in_lookahead plain lookahead_then_capture 24592/s -- -1% -2% capture_in_lookahead 24782/s 1% -- -1% plain 24989/s 2% 1% --