[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Ugly regexps
From: |
Stefan Monnier |
Subject: |
Re: Ugly regexps |
Date: |
Wed, 03 Mar 2021 10:48:33 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
>> (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.
Oh, thanks for the pointer.
> Its goal was to move in the other direction, but (unless I'm missing
> something about the syntax differences) this function is reversible.
Indeed it is,
Stefan
Re: Ugly regexps, Helmut Eller, 2021/03/03
Re: Ugly regexps, Dmitry Gutov, 2021/03/03
- Re: Ugly regexps,
Stefan Monnier <=
Re: Ugly regexps, Lars Ingebrigtsen, 2021/03/03