Ok you are right,
so because i have no patience at all with regexes you can exploit the fact that your valid strings are always odd; they start and end with x and another x must be in the middle.
use strict;
use warnings;
while (<DATA>){
chomp;
# note the string IS always odd
my $inter = int ((length $_) / 2)-1;
my @char = $_=~/./g;
if (scalar @char % 2 < 1){
print "Not OK $_ (unbalanced)\n";
next;
}
if (
$char[0] eq $char[$inter+1] and
$char[0] eq $char[-1] and
$char[0] eq 'x'
){
print "$_\t\tOK\n";
}
else {
print "NOT OK $_\t[$char[0] $char[$inter+1] $char[-1]]\n";
}
}
__DATA__
xxxxx
x1x2x...x
xxx
x.x.x
x12x..x
x123x...x
x123x.x.x
x12x1x
# out
xxxxx OK
x1x2x...x OK
xxx OK
x.x.x OK
x12x..x OK
x123x...x OK
x123x.x.x OK
Not OK x12x1x (unbalanced)
L*
UPDATE: it can be semplified, or golfed, a lot using 5.010
use strict;
use warnings;
use 5.010;
while (<DATA>){
chomp;
# note the string IS always odd
if ((length $_) % 2 < 1){
print "Not OK $_ (unbalanced)\n";
next;
}
if (($_=~/./g)[0,(int((length $_)/2)-1),-1]~~[qw(x x x)]){
print "$_\t\tOK\n";
}
else {
print "NOT OK $_\n";
}
}
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|