help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bind-string-match: comments please


From: Andreas Politz
Subject: Re: bind-string-match: comments please
Date: Tue, 30 Sep 2008 20:30:15 +0200
User-agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724)

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]