in reply to Checking for occurrence in comma separated string
my @list = split /,/, $thestring; if(grep { $_ == $thecheck } @list) { print "Found it!\n"; } # Or, if you need this a lot: # preparation: my %set; $set{$_} = 1 foreach split /,/, $thestring; # actual test: if($set{$thecheck}) { print "Found it!\n"; }
If you do insist on using a regular expression, you need to be aware that a number can appear at the beginning or at the end of a string. Example check:
but I prefer the double negative:if($thestring =~ /(?:^|,)$thecheck(?=,|$)/) { ... }
(If there's anything in front or right after the match, it may not be anything other than a comma.)if($thestring =~ /(?<![^,])$thecheck(?![^,])/) { ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Checking for occurrence in comma separated string
by Delusional (Beadle) on Oct 25, 2005 at 11:16 UTC | |
by bart (Canon) on Oct 25, 2005 at 11:28 UTC |