Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

cool regular expression needed for matching in columns

by Ben Win Lue (Friar)
on Jan 24, 2007 at 10:03 UTC ( [id://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

Replies are listed 'Best First'.
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 (Chancellor) 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 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
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-24 00:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found