Home > Archive > PERL Beginners > July 2005 > wildcard matching
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| hi
i have a bit of a puzzling question. well, puzzling to me anyway.
i have a sub which is passed 2 arguments. a wildcard search, and an actual
string to search in.
the wildcard search could be anything like an exact match with no wildcards
in, or a proper wildcarded search. such as:
*.co.uk
someones.hostname.com
etc and it has to search through a list of hosts and return only the ones
that match the search string supplied.
the sub i have is:
sub wcmhost {
$temp1 = shift;
$temp2 = shift;
($temp1 = quotemeta $temp1) =~ s/\\\*/.*/g;
if ($temp2 =~ $temp1) { return 1; }
else { return 0; }
}
whereas this is all very well, but if i search for *.uk, it won't just
return hostnames ending in ".uk", but it will return a host such as
"this.is.my.ukhostname.net", matching the ".uk" inside it too. what
modifications do i need to make to this sub so it only matches what's given,
and wildcards where there's a *?
temp1 is the search string, temp2 is the actual string it has to search in.
the sub is called from within a foreach loop which loops through a list of
hostnames, and responds according to the 1 or 0 this sub returns.
thanks for all your help,
dan
| |
| John W. Krahn 2005-07-24, 8:29 pm |
| dan wrote:
> hi
Hello,
> i have a bit of a puzzling question. well, puzzling to me anyway.
> i have a sub which is passed 2 arguments. a wildcard search, and an actual
> string to search in.
> the wildcard search could be anything like an exact match with no wildcards
> in, or a proper wildcarded search. such as:
> *.co.uk
> someones.hostname.com
> etc and it has to search through a list of hosts and return only the ones
> that match the search string supplied.
>
> the sub i have is:
> sub wcmhost {
> $temp1 = shift;
> $temp2 = shift;
> ($temp1 = quotemeta $temp1) =~ s/\\\*/.*/g;
> if ($temp2 =~ $temp1) { return 1; }
> else { return 0; }
> }
> whereas this is all very well, but if i search for *.uk, it won't just
> return hostnames ending in ".uk", but it will return a host such as
> "this.is.my.ukhostname.net", matching the ".uk" inside it too. what
> modifications do i need to make to this sub so it only matches what's given,
> and wildcards where there's a *?
> temp1 is the search string, temp2 is the actual string it has to search in.
> the sub is called from within a foreach loop which loops through a list of
> hostnames, and responds according to the 1 or 0 this sub returns.
It looks like you need this module:
http://search.cpan.org/~rclamp/Text...ib/Text/Glob.pm
John
| |
| Jeff 'japhy' Pinyan 2005-07-24, 8:29 pm |
| On Jul 22, dan said:
> the wildcard search could be anything like an exact match with no wildcards
> in, or a proper wildcarded search. such as:
> *.co.uk
> someones.hostname.com
Do you plan on allowing the '?' wildcard to match any single character?
Just curious how robust this should be.
> sub wcmhost {
> $temp1 = shift;
> $temp2 = shift;
> ($temp1 = quotemeta $temp1) =~ s/\\\*/.*/g;
I would instead say:
$temp1 =~ s{([^\w\s])}{
if ($1 eq '*') { '.*' }
else { "\\$1" }
}eg;
I believe that's somewhat safer.
> if ($temp2 =~ $temp1) { return 1; }
> else { return 0; }
You want to anchor the regex with ^ and $:
if ($temp2 =~ /^$temp1$/) { return 1 }
else { return 0 }
And while we're at it, you could just write:
return $temp2 =~ /^$temp1$/;
> }
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
|
| this worked a treat. thank you so much.
i wasn't planning on using ? to match a single character, i figured that
would be getting too complicated. how would that be done anyway? i must say
my regex skills aren't very good :/
dan
"Jeff 'japhy' Pinyan" <japhy@perlmonk.org> wrote in message
news:Pine.LNX.4.61.0507230039400.23911@perlmonk.org...
> On Jul 22, dan said:
>
wildcards[color=darkred]
>
> Do you plan on allowing the '?' wildcard to match any single character?
> Just curious how robust this should be.
>
>
> I would instead say:
>
> $temp1 =~ s{([^\w\s])}{
> if ($1 eq '*') { '.*' }
> else { "\\$1" }
> }eg;
>
> I believe that's somewhat safer.
>
>
> You want to anchor the regex with ^ and $:
>
> if ($temp2 =~ /^$temp1$/) { return 1 }
> else { return 0 }
>
> And while we're at it, you could just write:
>
> return $temp2 =~ /^$temp1$/;
>
>
> --
> Jeff "japhy" Pinyan % How can we ever be the sold short or
> RPI Acacia Brother #734 % the cheated, we who for every service
> http://japhy.perlmonk.org/ % have long ago been overpaid?
> http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Jeff 'japhy' Pinyan 2005-07-24, 8:29 pm |
| On Jul 23, dan said:
> i wasn't planning on using ? to match a single character, i figured that
> would be getting too complicated. how would that be done anyway? i must say
> my regex skills aren't very good :/
The ? metacharacter would map to Perl's . regex metacharacter.
[color=darkred]
If you wanted to support it here, you could end up doing
$temp1 =~ s{([^\w\s])}{
if ($1 eq '*') { '.*' }
elsif ($1 eq '?') { '.' }
else { "\\$1" }
}eg;
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
|
|
|
|
|