4 ms·
> Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity" I don't think this is true - i
by sweezyjeezy 5y ago
> Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity"
I don't think this is true - it's because people don't write regex every day - I used to write a lot of web-scrapers which involved getting pretty handy with regexes, it's fine, trust me. If a regex is REALLY hard to read - it's normally because it's badly written, or because it's trying to do something genuinely complicated.
Also how are they not modular? You always build up a regex from smaller regexes, maybe the modularity isn't always easy to follow, but it is there. e.g.
{regex1}{regex2} = regex1 followed by regex2 (concatenation)
({regex1}|{regex2}) = regex2 OR regex2 (OR operator)
etc.
Your URL example can be broken down into smaller modules - e.g. there's a 3-way OR block at the start that cover http(s)://, www(123)., abc.aa/ (don't understand the last one..)
- jimsimmons 5y agoModularity exists but it's not made explicit. I need to mentally track the level of nesting, column number, suffixes (*+?) etc. If the structure was made explicit, it would be a lot more readable. And expressing the composition gradually, bottom up, comes very close to the procedural specification.
- sweezyjeezy 5y agoThere's no chance you're going to convince me that throwing out regexes in favour of writing EVERYTHING procedurally is a good idea. I fully agree that they should often be built programatically to improve readability and reduce duplication. But capture groups, lookahead operators, kleene stars - these are a drag to do manually, not to mention you lose regex compiler optimisations - regex is the way.
- renewiltord 5y agoActually, what is the modularity concept in regexes? Is there a regex extension language where I can say "first match this then match that" but where I can name each part appropriately?
- ModernMech 5y agoAt some point, you’re probably better off using an actual parser. Parser combinator libraries make writing such things very easy.