8 ms·
I'm confused when people call it simple or elegant. I wonder if the same crowd would use similar words to describe regex.
by jimsimmons 5y ago
I'm confused when people call it simple or elegant. I wonder if the same crowd would use similar words to describe regex.
- gliptic 5y agoCompared to writing the parsing in procedural code? Yes. e.g.: a(b|c)d vs something like: return str.StartsWith("a") && str.EndsWith("d") && str.Length == 3 && (str[1] == "b" || str[1] == "c"); Why wouldn't you consider regular expressions simple and elegant for the problem they are solving?
- parhamn 5y agoMaybe they mean syntax? I guess you could compare it to say syntax like "char 'a' + either char 'b' or char 'c' + char 'd'". I like regex as is but that would def make it more approachable for the folks I know who don't.
- jimsimmons 5y agoThat's a simple example. Things quickly get out of hand once you have a more complex problem like recognising a valid URL [1]: `r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s` Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity". Functional programs also tend to pack many variables or operations in the same line without delimitation. For example Haskell's argument syntax. [1] https://www.geeksforgeeks.org/python-check-url-string/ https://www.geeksforgeeks.org/python-check-url-string/
- SOLAR_FIELDS 5y agoSure, but I think your example goes way too far in the other direction. URLs are quite complex in comparison to a lot of pattern matching problems. Also, what would be the procedural equivalent of the regex you linked? My guess is that if such a solution existed in procedural code, that this symbol soup you linked would be considered simple and elegant in comparison.
- jimsimmons 5y agoProcedural version is more verbose and quite long. That's the downside but it's also the reason why it'd be more readable and maintainable. Procedural version would actually be very similar to a parser combinator with elementary parsers being things like `while str[i].isalphanumeric(): i++`.
- gliptic 5y agoOr you could write it as a PEG (or indeed parser combinators) that would be even easier to read. I don't buy that verbosity on its own makes something readable and maintainable.
- jimsimmons 5y agoImagine comparing diffs of a verbose parser vs diff between two complex regexes. The parser one could be 10x larger or even more, but it'd have a tractable diff. In the regex case I'd have to mentally keep track of column numbers and a bunch of other things.
- gliptic 5y agoPEG doesn't have that problem.
- Zababa 5y ago> Things quickly get out of hand once you have a more complex problem like recognising a valid URL I'm not sure it's so complex. What's the equivalent code for recognising an URL? Regex are implemented in many languages, so this regex is, in a way, a portable library/function in most programming languages. And since it's shared, it can easily be checked way more than any single library would be.
- jimsimmons 5y agoPortability is an orthogonal concern. I agree with you that it's the reason regexes are the way they are but we could have also had better cross platform APIs.
- Zababa 5y agoRegexes are still better than cross platform APIs, they're an abstract concept. Just like you can implement a tree or a graph in any language and transfer your "tree skills" or your "graph skills", you can do so with regexes.
- mcdonje 5y agook, now do the same check in procedural code like the comment you responded to did.
- hobs 5y agoAd absurdum - what do you think is easier to memorize - a story (eg The Iliad) or an equivalently explanatory regular expression?
- mcdonje 5y agoJust trying to get an apples to apples comparison. He's using a relatively involved problem to say regex is difficult to parse. But the alternatives are libraries that hide the complexity, or large code blocks if you're rolling your own.
- 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?
- 5y ago
- threatofrain 5y agoTo be really persuasive, I think we'd all like to see what a well-written URL checking procedure would look like when optimized for human reading.
- dekhn 5y agoregular expressions have structure. That's why you can concatenate two regular expressions a b to ab and you have a resulting expression that will match a string if a matches its first part and b matches the rest Mainly what you're showing is that regular expresisons are awkward for matching certain types of syntax. You could have split each of the unique components in your expression into small parts and it would be much more readable.
- jimsimmons 5y agoCompletely agree. My point is that having things broken down and built up gradually (aka procedural) is a better approach than one shot declarations.
- pizza 5y agoThis is basically code golfed. You could always break up a regex into named variables of its constituent logical parts; same with FP. This is kind of like calling js ugly because you could come up with a minified js script that is really complex and fits in one line, or if you inlined all function calls in a long C file into the main() function body. There's a sweet spot of the complexity density per LOC that humans are seemingly more-or-less tuned for, where it's often possible to get a computer to handle more complexity-dense code. Personally, the shorter the code and the stronger the types, the faster I can tell if I made a mistake and the fewer places there are for me to make one, and that's why I like terse FP languages.
- kristianbrigman 5y agoI personally like regex as well, but it can be hard to read… there are other similar things like python parse (https://pypi.org/project/parse/,”parse https://pypi.org/project/parse/,”parse is format in reverse”) which are a little easier for easy cases.
- staticautomatic 5y agoIf you aren’t writing verbose regexes and/or commenting the shit out of them, you’re really doing future self and others a disservice.
- ReleaseCandidat 5y agoI'd call return str == "abd" || str == "acd" _way_ more elegant and simple, to be honest. Because the actual code would look like return match("a(b|c)d", str)
- dekhn 5y agoerr.... regex, which are a part of a larger field of formal language processing, which is elegant (but not simple). https://en.wikipedia.org/wiki/Regular_language https://en.wikipedia.org/wiki/Regular_language The whole area of parsing and grammar is fascinating.
- jimsimmons 5y agoExpressivity is independent of specification. You can specify the same language in multiple ways and some are more readable than others
- dekhn 5y agoDo you know of a more readable regular language parser than regular expressions? News to me.
- chillfox 5y agoI watched a video explaining monads where the presenter explained that simple meant small… So yeah, I think they would call regex simple as well. It honestly never occurred to me before watching that video that someone could mean small when they say simple. I have always understood simple to mean “easy to understand” or “low complexity”.