in reply to A (non) reg-ex question
Here's one approach:
!/[^01]/ && !/10/ && ($_ ^ reverse $_) !~ /[^\001]/
That is: a) check that there are no characters that are not 0 or 1, b) check that no 1s precede 0s, c) XOR the string with its reverse - it should give chr(1) for every character.
Another approach, this time destructive:
1 while s/(?<!1)01(?!0)//g; length($_) == 0;
That is: delete "01" from the centre as often as you can, and verify that this deletes all characters from the string. The lookarounds guard against deleting from 101 or 010.
And another, using a single recursive regexp:
our $re; $re = qr{(?:0(??{$re})1)?}; /^$re\z/;
That is: MATCH := "" | "0" MATCH "1".
Update: added missing '/' in s///, per johngg.
Hugo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: A (non) reg-ex question
by tilly (Archbishop) on Mar 21, 2006 at 04:36 UTC | |
Re^2: A (non) reg-ex question
by johngg (Canon) on Mar 20, 2006 at 16:25 UTC | |
Re^2: A (non) reg-ex question
by johngg (Canon) on Mar 20, 2006 at 21:24 UTC | |
by hv (Prior) on Mar 20, 2006 at 23:51 UTC | |
by johngg (Canon) on Mar 21, 2006 at 00:11 UTC |