4 ms·
The regular formalism is, fundamentally, about composition, and the current syntax doesn't do it justice. That syntax was devised as a math notation, where sub
by pygy_ 4y ago
The regular formalism is, fundamentally, about composition, and the current syntax doesn't do it justice.
That syntax was devised as a math notation, where sub-patterns were abstracted as one letter variables. You didn't have to deal with complex patterns at all, and it was very readable for that use case.
It was then re-purposed as a write-only language for searching at the CLI (in `ed` and its descendants, then grep).
They gradually graduated into what they are today in general programming languages because they were familiar and worse is better... However, composition got lost in the process.
Swift takes a radically different approach, and gives RegExps a parser combinator syntax. So every sub-expression can be assigned to a variable and reused/tested independently.
Here's an example (sorry I don't have it at hand in text form): https://pbs.twimg.com/media/FipgjUdUcAAl1xS?format=jpg&name=large https://pbs.twimg.com/media/FipgjUdUcAAl1xS?format=jpg&name=...
I wrote a lib that does the same in JS (the lib predates the Swift syntax by years). Here's the same example ported to JS: https://flems.io/#0=N4IgtglgJlA2CmIBcBWAzAOgAwCYA0IAzgMYBOA9rLMgNoCMAnFhinlgLoEBmEChtoAHYBDMIiQgMACwAuYagWLlBM+CuQgAPFLQA+ADqCABEYAqU+EYBK8AOYBRAB4AHJIc0B6HbpAFC8BGIZCGV+CTokOjQQAF88IVFxSQArfkVlVXUJCDBnclIZI2BBchkAQQthKDwjETEoAGFhZxkAV1J4Gv8AR1a1Yk6jQlauHkcYoy4KMCMAciVc8n8AWg7beBdZw0MlQUJCgC94CgB5UgBZfMsAXiGRsYAKWYAqWYBKHdDCsGEATwAjG53UYQRxPAD870+e0KyngZ0uHSMt2GILBswA1FDBLt9kY4QirjZYK0gsIVMjgY9MZCPjivkYAHLwADuABkIIIgT0+jj4A8fgD+Vt9KR3jURYJsbjCsz2Zz4ABleC9fpAo6nC5XB5yjlcukyoxlQS-RnKXUKykeDAeVrbekwowAVVIsEpPLVT1kMmcsxqgsBT0I4rmSA8Hj9+K5hI6D2NpvNrL18DedOheIAbtB4OQmlR3SreQMHoYTB4AHoePClubLOvLB4ANWz5CMedgb3ryz9NYtXOVqr51eMRgD-JrJlmNB7I5MdXgjWabVjs3nkYJWo6xNJMnJMjjJrNgj7Kbew5Mk-YM4vTKTCoHRc6NbPNfni5a7WF7VgkZdHfPt7yv2hZqgBsz1tckH1teRgeAAJB4hhpg6eI-DIxBSJSWZQDm7YYBs8DECWggAAaGF2TYtm2whUJ29aGDQ7ZGOc8AAITsIY3rOIQYYeMQNGwPhjiiM4CAYAs5F1pB1z0aRGAyKQOQPHSyEQFwRgCsI6FSG8RQ1oaxSJDU34TLcaEYRgtgUK03H6aElDwBgsDkLYDzAN+NTzjEulGIYMT2oazhIrcUDkMQrRiCo4kdFp8D2AgkX7rMQXwNiKXyRsMgNBkaiFLc2G4QJhiheFiUYP85BQL8GDNM4ahQA8KVvL4RABIRwShBodA4EgOAAOyxPEIDzho4mEGkIC7JkMgaLE7AxEAA https://flems.io/#0=N4IgtglgJlA2CmIBcBWAzAOgAwCYA0IAzgMYBOA9...
Here's the lib: https://github.com/compose-regexp/compose-regexp.js https://github.com/compose-regexp/compose-regexp.js
- gawi 4y agoWhen I first encountered the Haskell Parsec library, the beauty of parser combinators hit me and I felt I'd never have to write a regular expression again. This hasn't been the case because I had to work in other languages where usage of regexes was more idiomatic/commonly understood. Parser combinators are great but most of the time, the problem can easily be solved using a regex and we can't really justify adding yet another library in the dependencies for the rare case it would be better suited. In addition to all the benefits they can provide in terms or readability, re-usability and abstraction, parser combinators can even compete with regexes in terms of performance. https://pl-rants.net/posts/regexes-and-combinators/ https://pl-rants.net/posts/regexes-and-combinators/