[Top][All Lists]

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

Ugly regexps

From: Stefan Monnier
Subject: Ugly regexps
Date: Tue, 02 Mar 2021 19:32:20 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

BTW, while this theme of ugly regexps keeps coming up, how 'bout we add
a new function `ere` which converts between the ERE style of regexps
where grouping parens are not escaped (and plain chars meant to match
an actual paren need to be escaped instead) to ELisp-style regexps?

So you can do

    (string-match (ere "\\(def(macro|un|subst) .{1,}"))

instead of

    (string-match "(def\\(macro\\|un\\|subst\\) .\\{1,\\}")



(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)
           ;; 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)) "")))

reply via email to

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