Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regexp experts, come to rescue!

by bman (Sexton)
on Feb 01, 2001 at 19:42 UTC ( #55716=perlquestion: print w/replies, xml ) Need Help??

bman has asked for the wisdom of the Perl Monks concerning the following question:

I have been already trying many approaches that would catch for me the following things:
-41
-55
-jj
1au
24u
36u
I have written the following regexp:
if ($MType =~ /-[45][15j]/ig || /[123][a46]u/ig || /7[9l][hb]/ig) { print "\"red\""; } else { print "\"#CCFFCC\""; }
All I'm getting is jj4. I have no idea where this comes from.

This is how I interpret my regexp:

1 regexp:  If the first char is '-' and second is 4 or 5 and third is 1 or 5 or j OR
2 regexp: If the first char is 1 or 2 or 3 and second is h or b and third is u <...> and so on.
Where is my logic flawed? I need some enlightment, PLEASE!

Replies are listed 'Best First'.
Re: Regexp experts, come to rescue!
by kilinrax (Deacon) on Feb 01, 2001 at 19:47 UTC
    One problem is you are performing the second and third regexps on the default variable '$_'. I.e. what your code is actually doing is this:
    if ($MType =~ /-[45][15j]/ig || $_ =~ /[123][a46]u/ig || $_ =~ /7[9l][hb]/ig ) { print "\"red\""; } else { print "\"#CCFFCC\""; }
    I would also offer '$MType =~ /^(-(41|55|jj)|(1a|24|36)u)$/' as a regexp that would match all the cases you list (and no others).
    However, if they are the only cases you want to match, creating a hash and checking for the existence of keys would be more efficient:
    my %matches; foreach (qw( -41 -55 -jj 1au 24u 36u)) { $matches{$_} = ''; } if (exists $matches{$MType}) { print "\"red\""; } else { print "\"#CCFFCC\""; }
      In that line, you may be better of using
      sub match{ my $MType = shift; $_ eq $MType and return 1 for ( qw(-41 -55 -jj 1au 24u 36u )); }

      Hope this helps,

      Jeroen
      "We are not alone"(FZ)

Re: Regexp experts, come to rescue!
by arturo (Vicar) on Feb 01, 2001 at 21:16 UTC

    I'm not sure I understand what you want your regex to match: JUST THOSE THINGS IN THE LIST, or things of the same form as the ones in your list? It makes a difference, because if you think of the *patterns* you want to match first, then it's easier to craft the regex.

    Anyhoo, just for fun, let's match just those things you've listed. I have a trick I use for short lists of different patterns, using join and an array of patterns I want to match:

    my @patterns = qw(-[45j]{2} [1-3][a46]u); my $pat_string = join "|", @patterns; if ($MType =~ /^$pat_string/oi) { # do stuff }

    yeah, those regexen are ugly, aren't they? But here's what they say:

    1. match a - followed by 2 4s, 5s, or js
    2. match the digits 1-3, followed by an a, 4 or 6, followed by a u

    The join gives you alternation (yields up a match if the string contains either pattern); the /oi says to compile the regex once (not really necessary, but hey, it's a feature, so we use it)

    I do not recommend this for code that needs to be optimized (alternations are generally slower than the system you're trying, where you try matching first one thing, then the other).

    But I'm too lazy just at the moment to produce the more optimized version of this code (hint: it involves looping over the patterns, maybe using eval if you're really militant about optimizing)

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Thanks Arturo for the tip! Why did I not think about it, I have no idea...

      In any case, I finally made it work. Most of the times, the problem stairs right at you but you simply don't see it.

      So, what I did whas this:

      my @patterns = qw(-[45j][15j] [1-3][a46]u 41u 57h 7[9l]b 8[046]u 9[ab] +t [1l][0b][uv] no [ou][j249][cdeg]); my $pattern = join "|", @patterns; # Arturo's suggestion my ($count) = 0; my $rm = 0; while (($CompName, $Department, $LogonDate, $NodeTel, $Model, $MTy +pe, $SinNum, $Room, $Name, $Division, $freespace, $userID) = $sth->fe +tchrow_array ()){ my $newpattern = $MType; $mySkip = $count % 2; $count = $count + 1; print "<tr"; if ($newpattern =~ /^${pattern}$/oi) { $rm++; print " bgcolor=\"red\""; $newpattern = shift; } elsif ($mySkip eq 0) { print " bgcolor=\"#CCFFCC\""; } else { print " bgcolor=\"white\""; } print ">\n"; }
      The problem I had was two-fold (or maybe even not. I think I was eluded by another bug I discovered in the process).
      • The 'if' pattern matching had an incorrect conditional. Instead of:
        if ($mypattern =~ /$pattern/ig) { ... } elsif ($count eq 0) { ... } else { ... }
        I had it in the wrong order (lookup my first post).
      • Modifying my pattern match also helped.
      In essence, everything is working now the way it's supposed to.

      Thanks monks!

Re: Regexp experts, come to rescue!
by mirod (Canon) on Feb 01, 2001 at 20:08 UTC

    Apart for the previous problem you should also anchor your regexps:

    /^-[45][15j]$/ # note the ^ and $

    otherwise you will match the regexp anywhere in the string: "toto-45tata" would match

    Then your interpretation is right, otherwise it would be
    if any character is '-' followed by 4 or 5 followed by 1 or 5 or j...

Re: Regexp experts, come to rescue!
by bman (Sexton) on Feb 01, 2001 at 20:07 UTC
    O.K. I have modified my regexp in the following way (I want to focus first on less detail to see what's happening):
    $MType =~ s/\s+//g; if ($MType =~ /^(?:-41)$/i || $MType =~ /^(?:-55)$/i || $MType =~ /^(?:-jj)$/i) { print "\"red\""; } else { print "\"#CCFFCC\""; }
    This however, does not return any results whatsoever now. I split it into three individual componets because I wanted to see if the engine will match that way. No go.
Re: Regexp experts, come to rescue!
by dsb (Chaplain) on Feb 01, 2001 at 20:39 UTC
    Maybe something got botched up in the value assigned to the scalar '$MType'. Post more of your code and maybe we'll catch a mistake some where preceding this regex. - kel -

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-07-07 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?