6 ms·
"This regular expression has been replaced with a substring function." This should be the title of a book on software engineering.
by smrtinsert 10y ago
"This regular expression has been replaced with a substring function."
This should be the title of a book on software engineering.
- AceJohnny2 10y agoThis regular expression has been replaced with a substring function. God I wish all my bugs were this easy to fix and deploy
- meshko 10y agoi wish people would stop using regular expressions in situations where they can be replaced with a substring function.
- zamalek 10y agoNick explained on Reddit why the regex was used[1]: > While I can't speak for the original motivation from many moons ago, .Trim() still doesn't trim \u200c. It's useful in most cases, but not the complete strip we need here. This would have probably been my train of thought (assuming that I consider regex to be a valid solution): Trim() would have been the correct solution, were it not for that behavior. Substring is therefore the correct solution. Problem is, IndexOf only accepts a char array (not a set of some form, i.e. HashSet). You'd need to write the <Last>IndexOfNonWhitespace methods yourself. Use a regex and make sure that it doesn't backtrace, because it's expressive and regex "is designed to solve this type of problem." The real problem/solution here isn't substring, it's finding where to substring. I consider regex too dangerous to use in any circumstance, but I can certainly see why someone would find it attractive at first. [1]: https://www.reddit.com/r/programming/comments/4tt6ce/stack_exchange_was_down_because_of_an_innocent/d5kis12 https://www.reddit.com/r/programming/comments/4tt6ce/stack_e...
- meshko 10y agoOh totally. I assumed that unicode bs immediately. And anyone would make this mistake easily. That's the point -- gotta have it imprinted in the brains, that regexes are for finding things in files, not for your production code. I've used them myself, but I'd like to think that when i type that regex in i stop and thing whether i will be feeding raw user inputs into it.
- Scea91 10y agoMany times regexes are more clear and therefore less bug prone than any non regex alternative. They have their use. Even in production.
- msoad 10y ago"I had a problem, solved it with RegExp, now I have two problems"
- StevePerkins 10y agoHeh... as a Java developer, my favorite version of that joke is where you solve the problem with Java, and now you have an `AbstractProblemFactoryFactory`.
- bigiain 10y agoThere's quite a nice history to that one: http://regex.info/blog/2006-09-15/247 http://regex.info/blog/2006-09-15/247 It dates back to August 12, 1997! (Perhaps someone should encourage jwz to have a 20th birthday party for that at DNA?)
- judah 10y agoI've fixed so many bugs using regex, only to have to fix several bugs later. My current stance is, avoid regex if at all possible. Turns out, many of the things we use regex for is possible without. Often times, .Substring, .IndexOf, and using LINQ over strings is sufficient.
- mattmanser 10y agoRegexes are almost always a massive code smell. They should almost never be used, bad idea, bad implementation, hard to grok, hard to debug, hard to test, hard to spot. Whoever came up with them has surely been given the same honorary place in hell with Jon Postel, who invented the utterly disastrous "be liberal in what you accept", that has plagued all web developers for the last 20 years.
- flukus 10y agoHow are they hard to test? Given input x, expect output y. It's one of the easiest things in the world to test.
- Niksko 10y agoThe point of this post is that even though the regex behaves correctly (input x produces expected output y), you also need to consider performance constraints. Without fuzzing, it's going to be pretty difficult to come up with enough test cases to thoroughly test a regex.
- qw 10y agoWouldn't the same apply to a custom method? Why would code using a combination of indexOf(), substring() and trim() be any less foolproof on arbitrary data?
- deleted 10y ago[deleted]
- mattmanser 10y ago
- lilbobbytables 10y agoWhat exactly is a substring function, and what makes it different than a regex?
- solipsism 10y agoMy guess is they're searching for the first non-whitespace character, reverse-searching for the last non-whitespace character, and then using String.Substring to return only what's in the middle. As to why they're not using String.Trim (https://msdn.microsoft.com/en-us/library/t97s7bs3(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/t97s7bs3(v=vs.110)....), maybe it's because String.Trim doesn't seem to know about the 200c whitespace character.
- dingo_bat 10y agoFrom what I understood, trim would work perfectly. It's 200 spaces not a single 200 width character.
- solipsism 10y agoYou're misreading the regex. \u200c is a single whitespace character. http://www.fileformat.info/info/unicode/char/200c/index.htm http://www.fileformat.info/info/unicode/char/200c/index.htm
- dingo_bat 10y agoAlso, removing spaces from the start and end of a line is a fairly standard operation which is provided by every language's built in library.
- _pmf_ 10y agoHere, the application's notion of whitespace was more comprehensive that the standard library's.
- curryhoward 10y agoAlthough, many of them do not handle non-ASCII Unicode whitespace characters (which is what the StackOverflow regex was going for).