Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

cool regular expression needed for matching in columns

by Ben Win Lue (Friar)
on Jan 24, 2007 at 10:03 UTC ( #596228=perlquestion: print w/ replies, xml ) Need Help??
Ben Win Lue has asked for the wisdom of the Perl Monks concerning the following question:

Hi, followers of the right belief,

I have a nine character string that contains three columns of 3 characters and I have to find two strings in the string.

Ok, lets say I have to find 'abc' and 'xyz'

'abcmmmnnn' has to result true (abc is found in first column)
'mmmxyznnn' has to result true (xyz is found in second column)
'mabcmxyzm' has to result false (neigther abc nor xyz in the columns)

I can do it the easy way by splitting the string into substrings and doing 6 single eq or doing 2 greps in an array of the substrings.

But is there a cool regex way?

Thank you for your attention

Comment on cool regular expression needed for matching in columns
Re: cool regular expression needed for matching in columns
by davorg (Chancellor) on Jan 24, 2007 at 10:09 UTC
Re: cool regular expression needed for matching in columns
by shmem (Canon) on Jan 24, 2007 at 10:22 UTC
    Many ways... one is
    while(<DATA>) { chomp; print "$_ => @_\n" if @_= /(abc|xyz)(?=(?:...)*$)/g; } __DATA__ abcmmmnnn mmmxyznnn mabcmxyzm xyzfooabc __END__ abcmmmnnn => abc mmmxyznnn => xyz xyzfooabc => xyz abc

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: cool regular expression needed for matching in columns
by siva kumar (Pilgrim) on Jan 24, 2007 at 11:14 UTC
    Try this., This may resolve your issue. But I can't say this will be the best solution.
    if($str2=~ m/^(abc|xyz)\w{6}|^\w{3}(abc|xyz)\w{3}|^\w{6}(abc|xyz)$/){ print "true "; }else{ print "false"; }
Re: cool regular expression needed for matching in columns
by caelifer (Scribe) on Jan 24, 2007 at 16:37 UTC
    Could be somewhat off topic

    When dealing with fixed length records, it was recommended to use pack / unpack pair. See "Perl Best Practices" by Damian Conway for more. With this, here is another solution that reflects this recommendation:

    use strict; use warnings; my $spat = qr/(?:abc|xyz)/; while(<DATA>) { chomp; my $r = $_; my @flds = unpack "(A3)*", $r; print "$r => "; do { print "$_ " if /$spat/ } for @flds; print "\n"; } __DATA__ abcmmmnnn mmmxyznnn xyzfooabc mabcmxyzm

    -BR

Re: cool regular expression needed for matching in columns
by graff (Chancellor) on Jan 25, 2007 at 05:12 UTC
    Using unpack and a hash instead of regex could be cool too:
    use strict; my %pat = ( abc => 1, xyz => 1 ); while (<DATA>) { my $out = ''; for my $seg ( unpack( 'A3A3A3', $_ )) { $out .= "$seg " if ( $pat{$seg} ); } print "$.: $out\n" if ( $out ); } __DATA__ abcmmmnnn mmmxyznnn mabcmxyzm xyzabcmmm abcmxyznn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://596228]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-20 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (153 votes), past polls