Öznur tastan wrote:
>
> Hi,
> I have been trying to solve a problem which is about to drive me crazy.
> May be some one know the answer(hopefully:)
>
> I want to get all macthes of a pattern in a string including the overlaping ones.
> For example
> the string is "xHxxHyyKzDt"
> and the pattern is /^(.*)H(.*)K(.*)D(.*)$/
>
> so in one round of match $1=x  $2=xxHyy $3=z $4=t
> in another                      $1=xHxx $2=yy $3=x $4=t
>
>
>     while ($sequence=~/$pattern/g )
>     doesn't work I think becaue the matches are overlapping
>
>    while ($sequence=~/(?=$pattern)/g )
> also doesn't work
>

Hi Öznur.

The problem is that wildcards in regexes will match either the maximum
number of characters for a match to work (.*) or the mimumum (.*?) and
nothing in between. The only way I can think of to do this is to
put an explicit count on your first field and try all possible values,
like the program below. Others are likely to come up with something
neater.

HTH,

Rob



use strict;
use warnings;;

my $sequence = 'xHxxHyyKzDt';

foreach my $n (1 .. length $sequence) {

  next unless $sequence =~ /^(.{$n})H(.+)K(.+)D(.+)/;

  printf "\$1 = %-6s", $1;
  printf "\$2 = %-6s", $2;
  printf "\$3 = %-6s", $3;
  printf "\$4 = %-6s", $4;
  print "\n\n";
}

**OUTPUT

$1 = x     $2 = xxHyy $3 = z     $4 = t

$1 = xHxx  $2 = yy    $3 = z     $4 = t




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to