Multiplying by three is the same as shift + add. Essentially you have a binary adder with carry, or rather two carries: the input bit is also a carry. So you have an FSM with three states for the three possible carry values 0, 1, 2.

    00111
 +   00111
  --------
     10101

Let's start with a recursive regex with groups for each of the states.

qr/ (?(DEFINE) ( 00 (?1) | 11 (?2) | ) # group 1: carry = 0 ( 01 (?1) | 10 (?3) ) # group 2: carry = 1 ( 00 (?2) | 11 (?3) ) # group 3: carry = 2 ) \A(?1)\Z /x;
From here, we can inline the group 2 in g1 and g3:
qr/ (?(DEFINE) ( 00 (?1) | 11 01 (?1) | 11 10 (?3) | ) ( ) ( 00 01 (?1) | 00 10 (?3) | 11 (?3) ) ) \A(?1)\Z /x;
Now it should be obvious that the group 3 i.e. carry=2 state is entered with a 11 10 and left with 00 01. Rewriting the regex with repeats instead of recursion:
my $Regex_Pattern = qr/ \A ( 00 | 11 01 | 11 10 ( 00 10 | 11 )* 00 01 )* \Z /x;

ps. I'm assuming that the input string must have 2*n digits.


In reply to Re: Regular Expreso 2 by oiskuu
in thread Regular Expresso 2 by choroba

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.