emacs-devel
[Top][All Lists]
Advanced

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

Re: rx.el sexp regexp syntax


From: Eric Abrahamsen
Subject: Re: rx.el sexp regexp syntax
Date: Sun, 03 Jun 2018 09:40:35 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Helmut Eller <address@hidden> writes:

> On Sun, Jun 03 2018, Eric Abrahamsen wrote:
>
>>> Maybe we could start with an ordinary macro, e.g. (pcre "(a|b)"), which
>>> could translate Perl/Python regexp syntax to Emacs regexp syntax.
>>
>> I made something like that, but the only advantage is your example
>> above: open and close parentheses, and the vertical bar. Everything else
>> still has to be double-backslashed. It feels inconsistent, and isn't
>> that much of a benefit...
>
> Any benefit on a small example is bound to be small.  And yes, in
> practice most regexps fit on a single line. i.e. they are small.
>
> I'm not sure what you mean with "everything else" as in this example
> there's nothing else.  The example would translate to: "\\(a\\|\\b\\)"
> which, while it is one character shorter, is also quite ugly.
>
> Maybe you mean that things like \w cannot occur in ordinary strings
> without double-backslash.  Hmm.. that's indeed a problem.

Yes, that's what I meant: all the other backslash constructions. If we
still have to write "(atl|choo)\\1", it doesn't feel consistent, and I
think doesn't save much mental overhead.

#+BEGIN_SRC elisp
(defmacro pcre (str)
  (with-temp-buffer
    (insert str)
    (goto-char (point-min))
    (while (< (point) (point-max))
      (cond ((looking-at "\\\\\\([(|)]\\)")
             ;; Remove double backslashes.
             (replace-match "\\1"))
            ((looking-at "\\([(|)]\\)")
             (replace-match "\\\\\\1"))
            ;; Add parsing of comments and elisp forms here.
            (t (forward-char))))
    (buffer-string)))
#+END_SRC

I would do:

(defalias 'r 'pcre)




reply via email to

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