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

J. Bobby Lopez jbl at jbldata.com
Mon Apr 29 08:28:13 PDT 2013


You'd have to use () to get the back-reference, so using Mike's 
suggestion it would be:

-------
use feature say;
my $s = 'foo,bar,boo,baz';
my $match;

if ( ($match) = 'foo,bar,boo,baz' =~ /,?(foo),?/) { say $match }
-------

Or mine..

-------
use feature say;
my $s = 'foo,bar,boo,baz';
my $match;

if ( ($match) = 'foo,bar,boo,baz' =~ /[,]*(foo)[,]*/) { say $match }
-------


Unless of course I'm missing some other requirement/restriction.



On 13-04-29 11:18 AM, Indy Singh wrote:
> I want to extract the matched string into a variable.
> e.g.
> my ($match) = something
> Indy Singh
> IndigoSTAR Software -- www.indigostar.com
> *From:* Matthew Phillips <mailto:mattp at cpan.org>
> *Sent:* Monday, April 29, 2013 11:14 AM
> *To:* Indy Singh <mailto:indy at indigostar.com>
> *Cc:* Toronto PerlMongers <mailto:toronto-pm at pm.org>
> *Subject:* Re: [tpm] String matching at start or middle or end of list
> Hi,
> Maybe 'if ('foo,bar,boo,baz' =~ /,?foo,?/) { ... } ' is what you're 
> looking for.
>
> Cheers,
> Matt
>
> On Mon, Apr 29, 2013 at 11:09 AM, Indy Singh <indy at indigostar.com 
> <mailto: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 <http://www.indigostar.com>
>
>     _______________________________________________
>     toronto-pm mailing list
>     toronto-pm at pm.org <mailto: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

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


More information about the toronto-pm mailing list