4 ms·
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>|[^()
by pilif 11y ago
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.
- ngoldbaum 11y agoThe 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.
- jandrese 11y agoThe 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.
- emodendroket 11y agoWell, when the only tool you have is a hammer...
- kuschku 11y agoIf 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.
- jerguismi 11y agoHmm, 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.
- jarcane 11y agoSome 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."
- emodendroket 11y agoThis is actually a place where "whoever," rather than "whomever," would be correct, even in the most conservative style.
- IshKebab 11y agoYeah 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.