emacs-devel
[Top][All Lists]
Advanced

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

Re: modern regexes in emacs


From: Mattias Engdegård
Subject: Re: modern regexes in emacs
Date: Mon, 11 Feb 2019 23:12:47 +0100

10 feb. 2019 kl. 10.39 skrev Elias Mårtenson <address@hidden>:
> 
> While I'm sure that is true for lot of people (and for those, the newly 
> announced xr package helps here), others prefer to use the more compact regex 
> syntax. 
> 
> However, I don't think anyone would argue that the Emacs regex syntax has any 
> advantages compared to pcre. I certainly need to wade through the Emacs regex 
> manual every time I want to do slightly more advanced regex matching, 
> followed by lots of testing. 
> 
> When using regexes in regular editing (as opposed to elisp programming) it's 
> even worse. 
> 
> I'm most definitely in favour of pcre. 

Hello Elias,

Of course you should write "-?[0-9]+" when you need it! And for interactive use 
-- search-and-replace, say -- the conventional notations are not bad, since 
they are compact to write, you have the meaning all in your head anyway, and 
nobody is going to look at it later on.

Where rx shines is for the complex ones. I have written page-long regexps in 
Perl and Python, and despite the fact that both languages permit a "structured" 
regexp layout, they does not come close to rx when it counts: rx can be read, 
understood, maintained, evolved, and composed far better, and with fewer 
mistakes.

I agree that the Posix notation is probably better than the old-style version 
in Emacs since the former tends to be a tad lighter in backslashes. Some 
languages - OCaml, Python, etc -- have some form of string literal that avoids 
the need to escape backslashes, but fundamentally, regexps are not strings but 
an algebraic notation with values and operators, and deserve some kind of 
higher language-level support. Larry Wall understood that.

So I suggest you give rx a go next time you need to write a complicated regexp 
in Elisp. If you still find it too verbose, you can use short keywords, like 
`+' or `1+' instead of `one-or-more'. You can even speak a hybrid dialect by 
injecting little regexp strings inside a big rx expression with the `(regexp 
...)' syntax! Take a look at the big `gnu' matcher in compile.el (around line 
281) to see what that looks like.

Careful here -- rx is addictive, and you may very well come to use it more and 
more.




reply via email to

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