in reply to Fun Regex Exercise
Nice Challenge
Basically you need to check for any string which begins and ends with the same character, any sequence of 0s or 1s inside this will match the '10|01' criteria. You also need to account for '1', '0', '11' and '00' as these also meet the '01|10' constraint.
/ ^(0|1)$ # simple cases of 1 or 0, with zero 01|10 | ^1(0|1)*1$ # 1 .. anything .. 1 | ^0(0|1)*0$ # 0 .. anything .. 0 /x
my @tests = qw( 0 1 01 10 00 11 000 111 001 011 101 010 100 110 1001 1101 0101 1010 ); foreach (@tests) { print "$_ "; if (/^(0|1)$|^1(0|1)*1$|^0(0|1)*0$/) { print "passed\n"; } else { print "failed\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fun Regex Exercise
by Anonymous Monk on May 16, 2005 at 19:52 UTC | |
by Anonymous Monk on Oct 16, 2010 at 13:53 UTC |