develooper Front page | perl.golf | Postings from June 2007

Re: regexp quickie

Thread Previous | Thread Next
From:
Ronald J Kimball
Date:
June 27, 2007 12:38
Subject:
Re: regexp quickie
Message ID:
20070627193821.GE55426@penkwe.pair.com
On Wed, Jun 27, 2007 at 02:26:35PM -0500, Roy.Johnson@shell.com wrote:
> Mine (borrowing RJK's testing code): $1 will be the last letter
> (non-underscore) before or at the target location; $2 will be the first
> letter at or after the target location, or the last letter if no such
> letter exists.
> 
> for (qw/ A_Z_K_ A_____ _____K /) {
>   print "$_\n";
>   for my $n (1 .. 6) {
>     my $r = $n - 1;
>     print "$n: ";
>     print /^(?=.{0,$r}([^_]))?.{0,$r}.*?([^_])/
>       ? "[$1] ($2)" : "no match";
>     print "\n";
>   }
> }

I wish I'd thought of doing it that way!

If you want $2 to only ever be the first letter at or after the target
location, you can just tweak the second half of the regex:

/^(?=.{0,$r}([^_]))?(?:.{$r}.*?([^_]))?/

Ronald

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About