[tpm] String matching at start or middle or end of list

Mike Stok mike at stok.ca
Mon Apr 29 11:47:10 PDT 2013

On 2013-04-29, at 1:52 PM, "Indy Singh" <indy at indigostar.com> wrote:

> Hi All,
> Following Alans suggestion, I am going to repost the question with some test cases.
> I am looking for a way to match a string that appears in a comma separated list. The string may may appear at the start or middle, end or nowhere on a list. I have stumbled along and made this work in the past, now I am looking for the clean and proper way to do it.
> One of the unpleasant side effects of putting a list with alternatives at the start .e.g. '(^|,)' , is that it creates an undesired capture string. Is it possible to avoid creating the capture string?
> Test code below. Anywhere where ‘foo’ appears should match but ‘food’ should not match
> foreach $s ('foo,bar,baz', 'bar,foo,baz', 'bar,baz,foo', 'bar,food,baz') {
>     print "s=$s ";
>     my ($match) = $s =~ /some reg expression or perl code here to match foo/;

any { $_ eq 'foo' } split(/,/, $s)

if you're allowed to use List::MoreUtils 'any'

>     if ($match) {
>         print "match=$match\n";
>     } else {
>         print "no match\n";
>     }
> }
> Indy Singh
> IndigoSTAR Software -- www.indigostar.com
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm


Mike Stok <mike at stok.ca>

The "`Stok' disclaimers" apply.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20130429/55fadb8c/attachment-0001.html>

More information about the toronto-pm mailing list