[plt-scheme] Small problem with regex-replace*

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Jun 16 12:08:32 EDT 2006

On Jun 16, Doug Orleans wrote:
> Danny Yoo writes:
>  > > (regexp-match "([a-zA-Z])+" "hello world")
>  > ("hello" "o")
>  > 
>  > (The result for "([a-zA-Z])+" surprises me!)
> 
> I was surprised when I first ran into this too, but it is documented:
> 
>   When a single sub-expression occurs in a ``zero or more'' pattern
>   (``*'') or a ``one or more'' pattern (``+'') and is used multiple
>   times in a match, then the rightmost match associated with the
>   sub-expression is returned in the list.
> 
> Seems like it would be more useful to return a list of all the
> sub-expression matches, but maybe that breaks some guarantee about
> space consumption?

More likely it will require rewriting code.  I've never seen a regexp
device that doesn't do this.  Here's the quivalent Emacs surprise:

  (let ((str "hello world"))
    (and (string-match "\\([a-zA-Z]\\)+" str)
         (list (match-string 0 str)
               (match-string 1 str))))

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.