First there's the solution that kvale gave, perhaps the best simplest choice:
/^([01])[01]*$/
but there are some other possibilities using lookahead:
/^(?=(.))[01]+$/
/^(?=[01]+$)(.)/

I feel a benchmark coming up...

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) }, });
Result:
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% --
Well... "plain" appears to be slightly faster, but I wouldn't bother worrying about the difference. Whjat's a few percent, anyway?

In reply to Re: Regex failure interpretation by bart
in thread Regex failure interpretation by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.