Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: how to add more intelligence to grep functionality

by holli (Abbot)
on Jan 11, 2006 at 19:33 UTC ( [id://522544]=note: print w/replies, xml ) Need Help??


in reply to how to add more intelligence to grep functionality

hand rolled solution:
#!/usr/local/bin/perl use strict; my @array = ( 'account_trend', 'trend_report', 'revenuebytrafficker', 'ticketstatus', 'lostinventory', 'campaigndetail', 'campaignsummary', 'packagesummary', 'placementsummary', 'bookedbyaccount', 'bookedbysalesperson', 'accounttrends', 'salespersontrends', 'salessitetrend', 'placementperformance', 'packageperformance', 'forcastsummary' ); my @values = ( '??m*', 'reve*ker', 'accoun*end', ); for ( @values ) { print "mask: $_\n"; my $regex = mask2re ($_); print "re $regex\n"; print join ("\n", grep { /$regex/ } @array), "\n\n"; } #turns a simple mask into a regex where * means #none ore more chars amd ? means exactly one char sub mask2re { #replacements (after quotemeta) my %map = ( "\\*" => ".*?", "\\?" => ".", ); #quotema everything my $re = quotemeta ($_[0]); #replace * and ? by regex active statements $re =~ s/(\\[\*|\?])/$map{$1}/g; #return compiled regex return qr/^$re/; }
Output:
mask: ??m* re (?-xism:^..m.*?) campaigndetail campaignsummary mask: reve*ker re (?-xism:^reve.*?ker) revenuebytrafficker mask: accoun*end re (?-xism:^accoun.*?end) account_trend accounttrends


holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found