Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Regular Expression: search two times for the same number of any signs

by Anonymous Monk
on Nov 29, 2016 at 10:37 UTC ( [id://1176786]=note: print w/replies, xml ) Need Help??


in reply to Re: Regular Expression: search two times for the same number of any signs
in thread Regular Expression: search two times for the same number of any signs

Very nice approach ! But it does not work when in the first any sign part is a "x"
"x1x2x...x" # should be valid is x.{3}3x.{3}x, but is NOK OK
  • Comment on Re^2: Regular Expression: search two times for the same number of any signs
  • Download Code

Replies are listed 'Best First'.
Re^3: Regular Expression: search two times for the same number of any signs -- no regex at all (updated)
by Discipulus (Canon) on Nov 29, 2016 at 12:23 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176786]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-23 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found