[REBOL] Re: Parse versus Regular Expressions
From: lmecir:mbox:vol:cz at: 6-Apr-2003 0:08
Hi Joel,
> I've just tripped over a tidy example of a task that's trivial to
> solve if backtracking is available.
>
> Find a repeated group of characters at the end of a string (one or more
> characters, repeated two or more times consecutively). For example,
>
> "my doggyoggy"
> 11112222
>
> "has as as as as "
> 111222333444555
>
> "fleasssssss"
> 1234567
>
> Note that some cases can be solved in multiple ways:
>
> "fleassssss"
> 111222
>
> "fleassssss"
> 112233
>
> "fleassssss"
> 123456
>
> in which case any solution is OK.
>
> The Perl/Python/etc-style regular expression which tests for this
> situation and provides the first occurrence of the repeated group
> of characters is
>
> /(.+)\1+$/
>
> which, for those not familar with RE "line noise" ;-) can be read
>
> (.+) a group (the parens) of one or more (the plus)
> arbitrary characters (the dot)
> \1 ... followed by the same group previously matched
> + ... one or more times
> $ ... at the end of the string
>
> I'm really interested in how a PARSE expert would approach the same
> task. Any takers?
>
> -jn-
like this:
x1: [end: (x2: copy/part start end) x2]
x3: to-rule x1
x4: [start: skip x3 to end]
parse head reverse "my doggyoggy" x4
Regards
-Ladislav