in reply to Re: reg exp to find presence of value in a list of comma seperated list
in thread reg exp to find presence of value in a list of comma seperated list

Thank you kennethk. I am learning perl now. I found that this one also is working $commalist =~ /,$item,/. I know there might be more than one way to do this, but if you don't mind can you tell me the difference between the one you suggested and this one
  • Comment on Re^2: reg exp to find presence of value in a list of comma seperated list

Replies are listed 'Best First'.
Re^3: reg exp to find presence of value in a list of comma seperated list
by kennethk (Abbot) on Nov 17, 2009 at 22:06 UTC

    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.