Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

S5 and overlap

11 views
Skip to first unread message

Ph. Marek

unread,
Sep 21, 2004, 6:33:40 AM9/21/04
to perl6-l...@perl.org
> # With the new :ov (:overlap) modifier, the current rule will match at all
> possible character positions (including overlapping) and return all matches
> in a list context, or a disjunction of matches in a scalar context. The
> first match at any position is returned.
>
> $str = "abracadabra";
>
> @substrings = $str ~~ m:overlap/ a (.*) a /;
>
> # bracadabr cadabr dabr br

Maybe I'm wrong here, but I'd get
$str = "abracadabra";
bracadabr
cadabr
dabr
br
(so far identical), but then I'd also expect
bracad
cad
d
brac
c
br

which gets me to the question, if there'll be some elements multiple times in
the array (they should), and in which order they appear (first match to
(nth .. 1st) match, 2nd to (nth .. 2nd)) and so on ...

BTW: will
$str = "abracadabra";

@substrings = $str ~~ m:overlap/ a (.*) (b|d) /;
get some empty strings as well (I believe it should)?


Regards,

Phil

Ph. Marek

unread,
Sep 21, 2004, 6:37:09 AM9/21/04
to perl6-l...@perl.org
> > # With the new :ov (:overlap) modifier, the current rule will match at
> > all possible character positions (including overlapping) and return all
> > matches in a list context, or a disjunction of matches in a scalar
> > context. The first match at any position is returned.
> >
> > $str = "abracadabra";
> >
> > @substrings = $str ~~ m:overlap/ a (.*) a /;
> >
> > # bracadabr cadabr dabr br
>
> Maybe I'm wrong here, but I'd get
Just found the answer, sorry.

But that gets me to the next question, ie I don't understand the difference
between exhaustive and overlap.

Is it that overlap fixes the first point of the pattern match and does further
scanning for all possibilities, and exhaustive then *after* this processing
searches for another first point?


Regards,

Phil

Jonathan Scott Duff

unread,
Sep 21, 2004, 2:43:18 PM9/21/04
to Ph. Marek, perl6-l...@perl.org
On Tue, Sep 21, 2004 at 12:37:09PM +0200, Ph. Marek wrote:
> But that gets me to the next question, ie I don't understand the
> difference between exhaustive and overlap.
>
> Is it that overlap fixes the first point of the pattern match and does
> further scanning for all possibilities, and exhaustive then *after*
> this processing searches for another first point?

:overlap gives you all of the matches as if you'd anchored the pattern
at the first character, then the second, then the third, and so on until
there are no more characters. So, for

"abbabbabba" ~~ m:ov/a.*a/

It'll match "abbabbabba" at the first position, then fail on the two
"b" characters, and succeed on the next "a" giving you "abbabba", etc.

:exhaustive is the same but you get "in between" greediness results
too. For instance,

"abbabbabba" ~~ m:ex/a.*a/

when matched starting at the first character will give you "abbabbabba"
because of the greediness of *. But there are also "abba" and "abbabba"
that match along the way.

So ... here's a little table comparing the two (ignoring the
positions where there is no match). Maybe it'll help. Assuming $_
contains "abbabbabba", we have

Position m:ov/a.*a/ m:ex/a.*a/
0 abbabbabba abbabbabba
abbabba
abba
3 abbabba abbabba
abba
6 abba abba

You'll note that :exhaustive matches the string "abba" at different
locations. One thing S5 didn't mention was how to know at which position
each substring matched. I'm sure that's filed away in the magic $0
object though.

Hopefully, I'm being clear.

-Scott
--
Jonathan Scott Duff
du...@pobox.com

0 new messages