Re: Pattern Matching Question



Dax Mickelson schreef:

> I am having problems matching ALL possible matches of a string against
> another (very large) string. I am doing something like: @LargeArray
> = ($HugeString =~ m/$Head......../ig); Where $Head is an 8 character
> string. (Basically I want to get all 16 character long substrings
> out of $HugeString where the first 8 characters match $Head.)
>
> My problem comes about when (for example) I want to match a 16
> character string that starts with AAAAAAAA. Suppose
> $HugeString=AAAAAAAAAASDFGHJKL and $Head=AAAAAAAA I want
> @LargeArray[0]=AAAAAAAAAASDFGHJ, @LargeArray[1]=AAAAAAAAASDFGHJK, and
> @LargeArray[2]=AAAAAAAASDFGHJKL
>
> Right now I would only get @LargeArray[0]=AAAAAAAAAASDFGHJ
>
> What am I doing wrong? How do I get all matches?

The startpoint (for the next iteration) is beyond the last match.

You want the startpoint to move only one position with each match. There
are several ways to do that.
One way is to use zero-width look-behind and look-ahead assertions (see
perldoc perlre).

#!/usr/bin/perl
use strict; use warnings;

{ local ($,, $\) = (':', "\n");

$_ = 'xxxxAAAAAAAAAASDFGHJKLxxxx';
my $Head = 'AAAAAAAA';

print $Head, $1, substr($',0,7) while /(?<=$Head)(.)(?=.{7})/ig;
}

(remove the colon from '$,' when you grok it)

--
Affijn, Ruud

"Gewoon is een tijger."


.



Relevant Pages

  • Re: Pattern Matching Question
    ... > I am having problems matching ALL possible matches of a string against ... > My problem comes about when I want to match a 16 character ... Perhaps there is only one occurrence of 'AAAAAAAAAASDFGHJ' in @LargeArray? ...
    (perl.beginners)
  • Pattern Matching Question
    ... I am having problems matching ALL possible matches of a string against ... $HugeString where the first 8 characters match $Head.) ... My problem comes about when I want to match a 16 character ...
    (perl.beginners)
  • Re: Cell formatting issue
    ... the query process select Preserve cell formatting ... The character string should be ... than opening it intoExceland found the character string did ... Looking at the .xls file I do not see a difference ...
    (microsoft.public.excel.misc)
  • Re: Question about Descriptors
    ... > let you effortlessly do math with numbers that were stored in strings. ... If you moved a character string with numeric characters ... I think I missed the posting that defined string processing. ... String processing includes a native ability to define character string variables ...
    (comp.os.vms)
  • Re: Why character instead of numerics?
    ... Create Cursor Test ) ... > numeric (not numeric digits in a character string that looks like a ... if I decode a character string that is composed of various digits ...
    (microsoft.public.fox.vfp.queries-sql)