Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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

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
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"; }

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?
[marto]: Does the command line arg --product-version@ help?
[LanX]: xD
[erix]: 'systematic' is going to be hard to define I think
[marto]: Err no @
LanX Much enemy much ore
[Corion]: marto: Naah, my module doesn't care, but the users of my module might expect some things to work in Chrome that only work in later builds (or, as seems to be the case, don't work there either ;) )
[Corion]: But I really wonder - I can tell Chrome to go offline, but requesting pages over the network still succeeds. But if I manually click the "offline" checkbox, that's different :-/

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-12-12 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (335 votes). Check out past polls.

    Notices?