Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I usually feel quite dirty when using Regex, because I know it's dirty. More often than not, even string splitting might do the job better.
-
@kescherRant if regex can't solve your problem, then you just need more regex - conventional wisdom
-
Why is regex dirty? That's Like hating on C++ Just because you're not familiar with it
-
Lexter11745yHm, usually i don't need regex. When i can solve a problem without it even for longer code price, i'll do it.
Yes, i don't like regex. :) -
@PublicByte I think we all agree on the performance argument. That's easily testable with a short benchmark. However I'm still Missing an argument for it being dirty. I'd also say it's common sense to Not use regex when the problem is simple enough that a simple string split will suffice.
-
hitko31485yFor any string matching problem, regex is always slower and uses more memory than optimised purpose-made algorithm, but the catch is, an efficient regex implementation (e.g. PCRE) will still run in O(N) time for about 50% cases with average time complexity for the remaining 50% cases below O(3N), which is likely more efficient than anything you can write in under 5 minutes.
-
Regex is dirty because it effectively obfuscates code.
It trades clarity for brevity. -
hitko31485y@Lensflare str.match(/(foo|bar|meow)/g) is about million times clearer than writing a bunch of code to get an array of those words, in order in which they appear in original text.
Another reason to use regex: when you have a pattern for X, a pattern for Y, and a pattern for Z, you can simply concat those patterns to match XYZ, or concat them in different order to match ZXY. If you had matcher functions instead of patterns, you'd need to run all of them in order and make sure to skip part matched by first call when calling the next matcher function, then checking that second match starts immediately after first match. Again, if(str.match(regex(X+Y+Z))) is way clearer than
c = true;
(m1, i1) = matchX(str);
if i1 < 0 c = false
else (m2, i2) = matchY(str, i1 + len(m1))
if i2 != i1 + len(m1) c = false
else ...
if(c) ... -
He feels like a programmer when he's using declarative language rather than imperative language? That's not programming, obviously. Oh, the irony.. Then that means he doesn't know what programming actually is: the process.
-
@don-rager Regex isn't efficient and it has a cost on performance. It looks dirty because it's the same context as a code smell: something smells fishy because you're trying to hack your way around something by using magical symbols.
-
@CaptainRant again: everybody agrees in the Performance argument ;)
Solving a problem by using magical symbols? Sounds Like programming in general lol.
I often hear the argument "I don't Like python or ruby because of the magic in the background"... it's only Magic when you don't know what happens in the background and not knowing what happens is not valid criticism -
@don-rager I'm not criticizing regex, but I'm rather socially providing an answer of many as to why people seem to think regex is 'dirty'.
-
Root824935y@CaptainRant People usually see it as dirty because they can't read it. People made the same argument with Perl.
-
@Root I usually consider it a "dirty solution". Slapping it onto anything that's parsing anything usually just makes performance worse than it could be.
-
@kescherRant Perceived dirtiness of a regex term and performance of regex engines are two entirely different things
-
@don-rager I perceive it as dirty because it's not an optimal solution for many things.
-
@kescherRant there are no "optimal for every perceiveable case" solutions
https://devrant.com/rants/2387511/... -
@don-rager Yes, of course. That's why you should just use the most optimal solution suited to your problem.
-
mcalis2595yI'm not convinced regex is actually that slow, but there are a lot of slow implementations to be sure. Here's an excellent article: https://swtch.com/~rsc/regexp/...
Related Rants
-
netikras7- just do your job. Close this ticket already and go to the next one - It's just a 1 minute job.. Don't build...
-
AlgoRythm0"Oh, I hadn't thought of that. I'll use that. Thanks!"
-
opengenus3As a senior developer, I introduced a bug in the hiring system at the company I worked at and it took HR nearl...
Me (as a Senior developer): How will you solve this problem using regular expression?
Junior developer: *Explains*
Me: Good
Junior developer: I truly feel like a programmer when I code regular expressions
Me: Now, we have two problems.
rant
wk193