4 ms·
There two kinds of text search/parse problems: - The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard on
by AlexanderDhoore 7y ago
There two kinds of text search/parse problems:
- The really easy ones. A simple string search/split will do and a regex would be overkill.
- The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code.
Please don't use regexes in production software. Learn how to write simple parsing code.
- gravitas 7y agoI need to validate an input string from the user to ensure it's a 32 character hex (I'm validating it's a NAA format 6 WWN) and regex is just the easy, fast and efficient to do this. It's not a complicated regex and is quite readable to anyone with basic regex knowledge, why is this wrong?
- madelyn 7y agoCan you elaborate a bit on this please? I'd be interested in resources on writing better parsers! Quickly looking at the python standard lib (urlparse, shlex, etc) and Python packages (NLTK Treebank tokenizer), a lot of packages related to slicing, dicing and parsing strings use a mashup of regex and rule based code.
- IceDane 7y agoParser combinators are the way to go. I don't use python often enough to know if such a library exists for python, but I would assume so.
- cben 7y agoNo experience with them in python, but look also for PEG grammars. They are significantly simpler than the traditional tower of lexer + ambiguous limited lookahead grammar. Plus the memoized Packrat algorithm allows throwing in functions with custom conditions. (Somewhat like parser combinators, they also support custom logic.)
- IceDane 7y agoI couldn't agree more. I honestly find it rather baffling that someone would write a book on not just regular expressions in general, but one particular variant in one particular language. I've been writing code for over 15 years and if I said I had reached for regular expressions maybe 10 times in that entire time, I'm pretty sure I wouldn't be off even by an order of magnitude. There are very few cases where they're the appropriate tool, I think. They don't compose(unless you use a higher level library that lets you construct them declaratively maybe), they are hard to read and hard to debug and nearly impossible to extend. In my opinion, parser combinators are state of the art for building parsers. They can read like prose, are very easy to extend and usually easy to debug as well since you can easily get detailed error messages.
- quickthrower2 7y agoRegex is handy for client side JS validation of fields such as a field needs to be an integer etc. You could use parser combinator, but why combinators (pun!) when a simple regex will do?
- castis 7y ago> regex will result in fragile/hard to understand code Sure, if you dont know what you're doing.
- asicsp 7y agoI don't have experience with parsers, so I cannot comment how easy/difficult it is compared to regex. Regex can indeed lead to issues [1] but so could any other piece of code. So, I disagree that regex shouldn't be used in production. This article [2] by Jeff Atwood gives a balanced view of when to use regex and some nice tips. [1] https://new.blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/ https://new.blog.cloudflare.com/details-of-the-cloudflare-ou... [2] https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ https://blog.codinghorror.com/regular-expressions-now-you-ha...