Is it possible to change emacs' regexp syntax?

I love emacs. I love regexs. I hate emacs' regex syntax - the need to escape grouping parens and braces, that you dont escape literal parens, the lack of predefined character classes, etc.

Can I replace emacs' regex engine, or tweak some setting, so that when I use the Query-replace-regexp (or one of many other) feature I can use the syntax I program with in java/js/perl/ruby/etc...?

Edit: The subject was originally "how to change emacs' regex engine" which would not only change escaping rules and add character classes, but also (not mentioned in the post) add support for various common extensions (?...). Features like non-capturing parens: (?:...), match only if/if-not followed by: (?=...)/(?!...), and others. I don't believe (though happy to be corrected) these are possible with emacs' current regex engine, and no amount of syntax replacements will fix that.

The solution below addresses the original issues of escaping and additional char classes by replacing with syntax emacs understands. A second answer (now deleted) suggested advising (add a function to run at the start of another) emacs' regex function to do the replacement for all regex processing. The author quickly censored him/herself realizing that it would likely break much existing emacs code, and eventually the post was removed.

I would still like to change the regex-engine to one which supports extensions, but I agree that changing the escaping behavior universally would cause havoc I'm not willing to chase. Thus, I'm changing the subject to match the question and accepting the response.

It crossed my mind to change the engine to support common-syntax and extensions, advise the regex function to convert emacs-internal code to common-syntax, advise the interactive functions to convert my common-syntax to emacs-syntax (so it can be converted back to common)... but I think even RMS would recommend a fork before that.


You could define your own elisp function which modified the regexp and then passed it back to emacs. In pseudo-elisp:

(defun my-query-replace-regexp (regexp)
    ; modify regexp to replace ( with (, { with {, etc.
    (query-replace-regexp modified-regexp)
)

; rebind C-% to use new regexp function
(global-set-key "C-%" 'my-query-replace-regexp)

If using Python regular expressions for incremental search / replace, and for replace and query replace is sufficient, then visual-regexp-steroids is a good choice.

visual-regexp-steroids is an extension to visual-regexp which enables the use of modern regexp engines (no more escaped group parentheses, and other goodies!)... For now, Python and pcre2el are supported out of the box (tested on Linux and Windows).

It defaults to Python regular expressions.

One really nice feature is a live search / replace, for example starting with

one = 1
two = 2
three = 3
four = 4

you can swap the numbers and strings around like so:

在这里输入图像描述

It can be installed easily via MELPA. My .emacs is:

(require 'visual-regexp-steroids)
(define-key global-map (kbd "C-c r") 'vr/isearch-backward)
(define-key global-map (kbd "C-c s") 'vr/isearch-forward)
(define-key global-map (kbd "C-c l") 'vr/replace)
(define-key global-map (kbd "C-c q") 'vr/query-replace)

but obviously you can change to suit, and override the built-in key mappings for searching and replacing if you wish.

链接地址: http://www.djcxy.com/p/76974.html

上一篇: 贪婪的量词不支持他们的语言?

下一篇: 有没有可能改变emacs的regexp语法?