Reputation: 35
So I have 2 variables, one a short string and one a long string that should contain the short string. I have added some prints to the script however the match isn't working - could anyone shed any light on this for me:
print OUT "Omni: [$omnihostname]\n";
print OUT "API: $api\n";
print OUT "Match: Y\n" if ($omnihostname =~ /$api/i);
print OUT "Match: N\n" if ($omnihostname !~ /$api/i);
print OUT "-----------------------------------------------------------\n";
Here's the output:
Omni: [ASW02SLO]
API: deviceDetail@deviceId = 10401381@hostName = ASW02SLO@ipAddress = [other redundant text here]
Match: N
-----------------------------------------------------------
Thanks, Ben
Upvotes: 0
Views: 58
Reputation: 339786
It appears to me that you have your terms the wrong way around in the regex, i.e. you wish to determine if the shorter string$omnihostname
is contained in $API
, not the other way around.
As such, there's a better solution than regexes:
if (index($API, $omnihostname) >= 0); # Match: Y
If case-insensitivity is desired, wrap either or both arguments in lc(...)
as necessary.
NB: fc(...)
is preferred for case folding in later versions of Perl.
Upvotes: 4
Reputation: 6568
This is the way that I would do it:
use strict;
use warnings;
my $ss = 'string'; # your 'key'
my @ls = qw(longstring longstringlong longstri); # an array to search through
foreach(@ls){
chomp;
print "Match! $_\n" if /$ss/;
print "No match! $_\n" if ! /$ss/;
}
Match! longstring
Match! longstringlong
No match! longstri
Upvotes: 0