Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In general, when you are working on a problem and you think "let me use a regex for that" and then you come up with

    ^\s*[^\s()}]+(?<m>[^()]*\((?:\g<m>|[^()]*)\)[^()]*)*[^()]*\)[,]?$
to solve your problem, then you have IMHO come across a problem which you should not be solving using regular expressions.

Case in point is counting and balancing parentheses which is very easily done using a single loop over the string in question.



The worst part is that as soon as he mentioned regular expressions I knew exactly what the problem was.

Regexes are powerful and useful but also dangerous. People who don't thoroughly understand them and try to get fancy often run into problems like this. In general you shouldn't be using them to parse a computer language anyway, it is something you should be using a tokenizer/parser for.


If something can be done with regex, someone will do it with regex, and it will end up in a big project.

That’s why I weeped when support for named groups and backmatching was added.


Hmm, if I remember right, on our compiler course we wre taught to build tokenizer with regexpes. As far as I unrestand, it is quite valid tool for that.


Well, when the only tool you have is a hammer...


The issue seems to be that Atom uses regular expressions as part of the internal API it uses to separate Atom core from add-on modules.


Maybe the API needs to be changed to allow the module to return a function. After all, JS has first-class functions, so this would make total sense.


This. With a default implementation that falls back to current behavior.


Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. -- Jamie Zawinski

To which I might dare add, "... and so does whomever next has to read this code."


This is actually a place where "whoever," rather than "whomever," would be correct, even in the most conservative style.


Yeah I was disappointed when he got to the crazy regex. Realised it was an unreadable mess, but then didn't realise that the real fix is not to use regex.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: