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

Mike Stok mike at stok.ca
Mon Apr 29 08:56:37 PDT 2013


In terms of clarity I think that splitting the concerns doesn't add that much code

@list = split /,/, $s;
@matches = grep { $_ =~ /\Afoo\z/ } @list;

@list could be eliminated if you like.

Mike

On 2013-04-29, at 11:16 AM, "Indy Singh" <indy at indigostar.com> wrote:

> Splitting is an option, but I am looking to minimize the amount of code. So in this case I am looking to match only.
> 
> 
> Indy Singh
> IndigoSTAR Software -- www.indigostar.com
> -----Original Message----- From: Mike Stok
> Sent: Monday, April 29, 2013 11:15 AM
> To: Indy Singh
> Cc: Toronto PerlMongers
> Subject: Re: [tpm] String matching at start or middle or end of list
> 
> Do you need to match or could you just split the list and see if any of the elements are what you want?
> 
> Mike
> 
> On 2013-04-29, at 11:09 AM, "Indy Singh" <indy at indigostar.com> wrote:
> 
>> Hi All,
>> 
>> What is a good way to match a string that may appear at the start or middle or end of 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.
>> 
>> 
>> Example:
>> my $s = 'foo,bar,boo,baz';
>> my ($match) = $s =~ /(foo)/;
>> 
>> I am looking to match the following:
>> The start of a string or a separator followed by
>> A specified string followed by
>> A separator or the end of the string
>> 
>> What are the extra things I need to surround the match expression?
>> 
>> 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?
>> 
>> 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>
> http://www.stok.ca/~mike/
> 
> The "`Stok' disclaimers" apply.
> 
> 
> 

-- 

Mike Stok <mike at stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.






More information about the toronto-pm mailing list