35

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.

Comments
  • 11
    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.
  • 11
    @kescherRant if regex can't solve your problem, then you just need more regex - conventional wisdom
  • 2
    Why is regex dirty? That's Like hating on C++ Just because you're not familiar with it
  • 20
    The plural of regex is regrets.
  • 2
    Hm, 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. :)
  • 1
    @PublicByte true, but hat's a different problem than regex looking dirty
  • 1
    @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.
  • 5
    I love regex, but I agree that it's only rarely the correct tool.
  • 1
    Regex 👍
  • 4
    For 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.
  • 3
    Regex is dirty because it effectively obfuscates code.

    It trades clarity for brevity.
  • 5
    @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) ...
  • 2
    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.
  • 1
    @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.
  • 2
    @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
  • 1
    @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'.
  • 2
    @CaptainRant People usually see it as dirty because they can't read it. People made the same argument with Perl.
  • 1
    @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.
  • 4
    @kescherRant Pretty sure that's been stated ten and a half times already.
  • 1
    @Root yes. But it's a counterargument to your comment.
  • 3
    @kescherRant Part; not all.

    Whatever; does not matter.
  • 3
    @kescherRant Perceived dirtiness of a regex term and performance of regex engines are two entirely different things
  • 1
    @don-rager I perceive it as dirty because it's not an optimal solution for many things.
  • 2
    @kescherRant there are no "optimal for every perceiveable case" solutions

    https://devrant.com/rants/2387511/...
  • 2
    @don-rager Yes, of course. That's why you should just use the most optimal solution suited to your problem.
  • 2
    I'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/...
Add Comment