CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Wildcard string matching

 



alexm
Deleted

Feb 8, 2001, 12:29 AM

Post #1 of 2 (528 views)
Wildcard string matching Can't Post

I am trying to create a simple string matching subroutine that can match a string of wildcards (* and ?) to another string e.g. if...

$String is 'abc.test.com'
$Mask is '*.test.c?m'

the subroutine would return 1 as there is a match

but if the mask was '???.co.*' there would be no match and it would return 0

any help much appreciated



japhy
Enthusiast

Feb 8, 2001, 8:30 AM

Post #2 of 2 (525 views)
Re: Wildcard string matching [In reply to] Can't Post

The simplest thing to do is create a mapping of * and ? to Perl's regex notation:


Code
%map = ( 
'*' => '.*',
'?' => '.',
);

# now, convert characters as needed
$pattern =~ s/(\W)/$map{$1} || "\\$1"/eg;

if ($domain =~ /$pattern/) { match }

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives