reddit / emacs
how do i get emacs to use a sane syntax for regular expression?
r3dnaz · 2022-03-05 14:59
i had enough of trying to guess if my regular expression did not match anything because `\w` is not supported, or `\d` is not supported, or because i have to put a `\` before `{`, `?`, `(`, or `[`. i had enough of giving up and opening another editor just for searching or replacing. it does not matter which editor or which programming language. they seem to all have standardized on a common syntax. why can emacs not?
Alan_Shutko · 2022-03-05 15:41
It might be nice for someone to adopt https://github.com/joddie/pcre2el and update it to address issues, so that people have the option of using a different regular expression syntax.
To some degree, though, you always have to be concerned with which regex engine you're using, because there's still a lot of variation hidden in ones that seem otherwise the same. https://gist.github.com/CMCDragonkai/6c933f4a7d713ef712145c5eb94a1816
andreyorst · 2022-03-05 15:55
You can use `rx` which makes writing regular expressions very clear by using s-expression syntax and normal names. It then compiles it to an optimized regular expression, so you don't have to keep s-expression format if you don't want to.
Additionally to that, there's `M-x re-builder` which can help you dynamically build the regular expression, and you can toggle between different syntaxes, like `string`, `read`, and `rx`.
mmaug · 2022-03-05 17:59
Alternative RE syntax has been a regular discussion point on emacs-devel and elsewhere for the past 20 years that I have been an Emacs contributor. The challenge is the way in which buffer text is stored in Emacs and having a RE library that can handle anything but a contiguous block of text. There are Emacs packages that support PCREs and emulate native functions which address the 'window dressing' end of the problem, but then what do you do with the huge dependence of the built in packages and core editor which use the current BRE syntax.
A RE library that is more expressive but less performant than the current may be a benefit to editing users but if it now takes 3mins to font lock a short script, it's all for naught.
While the brain shift between BREs, EREs, AREs, and PCREs (and numerous flavors) is a challenge it is one we have been stuck with (see `grep`/`egrep` from the 1970/80s) and will continue to be stuck with, for a while.
If someone can develop a library that supports expressive REs and can handle thousands of searches efficiently, all while being flexible in the format of the source text, then there is nothing but accolades to be had (and millions of lines of code that'll have to be modified to take advantage of it)
mtk000 · 2022-03-05 18:30
BRE, ERE, and PCRE are all familiar. what is ARE?
emax-gomax · 2022-03-05 18:53
In elisp? Use rx.
In interactive stuff? Depends on what you're trying to do. For search, narrowing, etc. use something like orderless which I believe give a much friendly expression syntax for queries. For substition/replacing where you'll need more fine grained control, capture groups and what else I don't think there an alternative. Personally I've just adjusted to it. :/
mmaug · 2022-03-05 20:36
Advanced RE—POSIX
7890yuiop · 2022-03-06 00:31
> why can emacs not?
For much the same reason that "window" is not the same thing in Emacs that people expect if they are only familiar with modern applications -- Emacs predates such modern 'standards' as PCRE, and the way it was done in Emacs is deeply ingrained in Emacs.
Furthermore, even if Emacs regexps were more ERE (or even PCRE) than BRE, there would still be Emacs-specific features differentiating its dialect from others.
It's possible that this can be done, but it wouldn't be of any benefit to the people who already know how to write regexps in Emacs, and moreover I think it would introduce a *horrible confusing mess* if such syntax variations became allowable in elisp code so I fervently hope that would never happen. Interactive use I can understand more, but then people need to remember even more differences between "code" and "interactive" regexps than is already the case on account of string read syntax (which starts out as fine if you're not writing any elisp, but becomes a problem once you start learning that -- although in principle re-builder could be used to convert some hypothetical alternative interactive syntax to the code syntax, the same way it does for string/read/rx conversions, which might be good enough as a solution to that).
As others have said, this question crops up regularly. My take is: Just learn the syntax. People don't want to, but I don't believe it's actually difficult to remember if you make the effort. Use re-builder and I'd suggest using https://github.com/benma/visual-regexp.el for feedback, and just spend a bit of time learning, and the entire problem just goes away.
sebhoagie · 2022-03-06 04:59
Seconding re-builder. C-c C-w kills the regex with escaped characters depending on the input syntax used.
And real time match updates in the target buffer are uber helpful.
oantolin · 2022-03-06 07:05
You can get Emacs to use a sane regexp syntax simply by learning the Emacs regexp syntax! Just read `(info "(emacs)Regexps")`: the same syntax that seems insane now will seem perfectly acceptable once you *learn it*.
(Also, you cannot be using a wide variety of editors and programming languages if you think they all use the same regexp syntax. I guess most use either PCRE or Extended Posix, but (1) both of those are pretty popular, (2) you see Basic Posix sometimes too, (3) and some other great tools like Vim use their own syntax.)
flexibeast · 2022-03-06 09:56
> you see Basic Posix sometimes too
For example, GNU `grep`; EREs are available via the `-E` option (which `egrep` is a wrapper for, but `egrep` is deprecated).
SlowValue · 2022-03-06 11:05
[Here is a helpful overview of emacs regexps (Emacs Wiki).](https://www.emacswiki.org/emacs/RegularExpression)
Tommerd · 2022-03-06 14:38
For just find/replace stuff I found visual-regexp + visual-regexp-steroids amazing!
The first does what it says: visual find-replaces regexes. The second is the magic one though, it allows you to use Python regex syntax instead of the clunky elisp dialect, way better! In theory it also allows for different implementations but AFAIK there aren't any others, I'd personally would love a JS regex one.