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

Kwan Tamakanic quantum.mechanic.1964 at gmail.com
Mon Apr 29 11:25:53 PDT 2013


say 'foo,bar,baz' =~ /(?:^|,)(foo)(,|$)/
> foo
say 'bar,foo,baz' =~ /(?:^|,)(foo)(,|$)/
> foo
say 'bar,baz,foo' =~ /(?:^|,)(foo)(,|$)/
> foo

The regex is generally cleaner if you use \b instead of the other bits,
assuming that meets your requirements.

(?:...) is a non-capturing group, which is useful for delimiting
alternations or applying quantifiers.

It must be Monday, because everyone seems to be struggling with this. ?:^/

-QM


On Mon, Apr 29, 2013 at 7:17 PM, Antonio Sun <antoniosun at lavabit.com> wrote:

> that'll match cases like,
>
> bar,and foo etc,baz
>
> I'm still think my answer is the closest, given what's required in OP.
>
>
> On Mon, Apr 29, 2013 at 2:00 PM, Stuart Watt <stuart at morungos.com> wrote:
>
>> I've used \b for this. i.e., /\bfoo\b/ - the \b is a zero-width
>> word/nonword boundary, and I use it a lot to anchor at separators.
>>
>> All the best
>> Stuart
>>
>>
>> On 2013-04-29, at 1:52 PM, Indy Singh 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/;
>>
>>     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
>>
>>
>>
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/toronto-pm
>>
>>
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
>


-- 
-QM
Quantum Mechanics: The dreams stuff is made of
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20130429/984e076d/attachment.html>


More information about the toronto-pm mailing list