[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bind-string-match: comments please
From: |
weber |
Subject: |
Re: bind-string-match: comments please |
Date: |
Tue, 30 Sep 2008 12:22:45 -0700 (PDT) |
User-agent: |
G2/1.0 |
On 30 set, 15:30, Andreas Politz <poli...@fh-trier.de> wrote:
> weber wrote:
> > I've written this helper function:
>
> > (defun bind-string-match (regexp string &optional start)
> > (let ((ret (string-match regexp string start)))
> > (when ret
> > (catch 'end
> > (dolist (i (number-sequence 1 10))
> > (let ((match (match-string i string)))
> > (if match
> > (eval `(setq ,(intern (concat "$"
> > (number-to-string i))) ,match))
> > (throw 'end t))))))
> > ret))
>
> > so that I can access the match groups with $1.. instead of (match-
> > string 1 string).
> > Is there any recommendation against this type of functions? Other than
> > polluting the global namespace?
>
> I think sooner or later you get into trouble, if you don't clear
> the match variables. They could still hold values from 10 matches
> ago, while the current regexp doesn't match at all.
>
> > Also, can one rewrite it without the `eval'?
>
> The 'q' in setq stands for quoted. So, just using set should
> work.
>
>
>
> > TIA,
> > weber
>
> > PS: I'm not a Perl programmer!
>
> Maybe what you really want is some kind of macro ?
>
> (defmacro with-string-matches (string &rest body)
> `(let ,(mapcar (lambda (submatch)
> (list (intern (format "$%d" submatch))
> (match-string submatch (eval string))))
> (number-sequence 0 10))
> ,@body))
>
> (let ((str "abbbbc") idx)
> (setq idx (string-match "a\\(b+\\)c" str))
> (with-string-matches str
> (message "%s %s" $0 $1)))
>
> -ap
Yeah, I was drifting that way after recalling pg's anaphoric
macros....
Thanks a lot :)
-weber