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.
They use a regular expression pattern to define all or part of the text that is to replace matched text in the input string. The replacement pattern can consist of one or more substitutions along with literal characters. Replacement patterns are provided to overloads of the Regex.
Regular Expression Search In GNU Emacs, you can search for the next match for a regexp either incrementally or not. Incremental search for a regexp is done by typing C-M-s ( isearch-forward-regexp ).
Simple Search and Replace Operations When you want to replace every instance of a given string, you can use a simple command that tells Emacs to do just that. Type ESC x replace-string RETURN, then type the search string and press RETURN.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With