4 ms·
And yet regular expressions are a part of most programming languages as a library or built-in syntax. Why is that?
by icsa 4y ago
And yet regular expressions are a part of most programming languages as a library or built-in syntax.
Why is that?
- bear8642 4y agoProbably due to they're a great notation for the problem area which for regex is concisely describing text patterns. For example 'a*b' is any number of 'a's followed by 'b'. How else would you concisely state that?
- LAC-Tech 4y agoHow else would you concisely state that? Presumably people who hate array languages think all 3 character regexes should instead be big nested loops, so they are "readable".
- samatman 4y agoRegexes in the Unix tradition are a user interface as much as a programming language. Not that there's a sharp distinction, but it's almost a trite observation that regexes per se shine for ad hoc string searching but show their weakness when they start becoming parts of programs. When writing a program, I prefer to use a PEG, giving the less compact notation `'a'* 'b'` but also letting me say `'a'* b` and define b as its own rule, including recursion for the useful cases. It helps that it's more powerful, being little more than a formalization of the post-regular strategies used in Perl-style 'regular' expressions while embracing recursion. For '/' in vim, grep, wherever? Yeah regex is fine, that's what it was designed for.
- IshKebab 4y agoI can't remember the names but I've seen at least two alternative syntaxes recently that are a lot more readable. At least one of them fixed the issue of regex mixing up control in-band with data. So your example would be something like "a"* "b" Much more readable and less error-prone.
- bear8642 4y ago> the issue of regex mixing up control in-band with data Could you explain this? I don't quite understand what the problem is. Do you mean something like sed's regex substitute command?
- woojoo666 4y agoI believe they mean the operators and operands are all mixed up, eg in `ab`, and this makes it so you have to escape all sorts of characters, but if you split it into `"a" "b"` then the separation is clear
- IshKebab 4y agoI mean it isn't clear whether a character is a control character (* + ? [ ] - etc) or a literal character because they're all mixed up. The rules about which is which are too complex, extensive and varying. If you use syntax like "a"* "b" then it's really obvious - the stuff in quotes is literal text, everything else is control. Lots of formats make the same mistake, e.g. YAML.
- IshKebab 4y agoThey're very quick to write and they (in appropriate cases) would be quite difficult to implement otherwise (tedious state machine stuff). They are still massively overused though. Using a regex at all is a huge red flag. Sometimes they are appropriate, but not in 90% of cases in my experience. Anyway I'm not sure the same is true for K. At least for the given example the for loop was not exactly difficult to write.