Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Regexp experts, come to rescue!

by arturo (Vicar)
on Feb 01, 2001 at 21:16 UTC ( [id://55750]=note: print w/replies, xml ) Need Help??


in reply to Regexp experts, come to rescue!

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

Replies are listed 'Best First'.
Re: Re: Regexp experts, come to rescue!
by bman (Sexton) on Feb 02, 2001 at 01:36 UTC
    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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found