First, welcome to the Perl community. I hope you get as much enjoyment out of working with Perl as I have.
Your regular expression misses the case where either the first or last element of your list is the element you wish to check for. Consider:
#!/usr/bin/perl
use strict;
use warnings;
my $csv = 'value1,value2,value3';
my $value = 'value1';
if ($csv =~ /(?:^|,)$value(?:,|$)/) {
print "Yes\n";
} else {
print "No\n";
}
if ($csv =~ /[,]$value[,]/) {
print "Yes\n";
} else {
print "No\n";
}
Mine catches those cases since the first group ((?: ... )) matches on either the start of the string (^) or a comma and the second group matches on either the end of the string ($) or a comma. Since value1 is not surrounded by commas, yours will not find it. Note that both cases will fail if there is white space in the csv that is not in the $value.
You might also note there is no need to define the single-element character class [,] as opposed to just using the raw comma ,.
As a side note, please read Markup in the Monastery, since by not wrapping your code in code tags, you inadvertently converted [,] into a link - when this happens, monks frequently misread your intention.
|