emacs-devel
[Top][All Lists]
Advanced

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

Re: Ugly regexps


From: Dmitry Gutov
Subject: Re: Ugly regexps
Date: Wed, 3 Mar 2021 14:17:39 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0

On 03.03.2021 02:32, Stefan Monnier wrote:
(defun ere (re)
   "Convert an ERE-style regexp RE to an Emacs-style regexp."
   (let ((pos 0)
         (last 0)
         (chunks '()))
     (while (string-match "\\\\.\\|[{}()|]" re pos)
       (let ((beg (match-beginning 0))
             (end (match-end 0)))
         (when (subregexp-context-p re beg)
           (cond
            ;; A normal paren: add a backslash.
            ((= (1+ beg) end)
             (push (substring re last beg) chunks) (setq last beg)
             (push "\\" chunks))
            ;; A grouping paren: skip the backslash.
            ((memq (aref re (1+ beg)) '(?\( ?\) ?\{ ?\} ?\|))
             (push (substring re last beg) chunks)
             (setq last (1+ beg)))))
         (setq pos end)))
     (mapconcat #'identity (nreverse (cons (substring re last) chunks)) "")))

See also xref--regexp-to-extended, my last attempt at RE->ERE conversion, though woefully lacking in tests.

Its goal was to move in the other direction, but (unless I'm missing something about the syntax differences) this function is reversible.



reply via email to

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