4 ms·
Disagree. There isn't and shouldn't be any hard fast rule for this. Same as most things in programming. Do what makes sense. Also, [0-9.] not [0-9\.]
by hnolable 14y ago
Disagree. There isn't and shouldn't be any hard fast rule for this. Same as most things in programming. Do what makes sense.
Also, [0-9.] not [0-9\.]
- eli 14y agoIMHO, if your intent is to capture a literal dot then you should escape it even if, based on the context, you don't have to.
- StavrosK 14y agoI agree with this. Regexps in general encourage a bit of "if unsure, escape".
- ralph 14y agoThen you could be introducing errors into your regexps. \x doesn't mean a plain x instead of the metacharacter x in all cases. Don't ship code you don't at least think you understand.
- ralph 14y agoNo, that's wrong, a reader that knows his onions, e.g. me, will be puzzled as to what the writer's intent was and spend time investigating if there is an error before deciding the writer needs to spend more time studying his onions.
- logn 14y agoAgreed. Regexes are already confusing. Adding unnecessary escaping just makes them worse. And if you have [\.] was the intent to escape the dot or match a slash? It's confusing.
- eli 14y agoI think the goal should be clarity, not conservation of characters. And I think the escaped dot is clearer (the author's intent is obviously to include a dot in the range). I do think it's funny that you said "There isn't and shouldn't be any hard fast rule for this" and then one comment later said my approach is "wrong" :) (EDIT: oops, that wasn't you -- Sorry!)
- logn 14y agoIt's only clear for people who don't really understand regexes. For people with knowledge of them, it's more ambiguous as to whether a slash is being matched or used to escape. It reminds me of commenting the end of your for loops, } //end for ... it's clear for people who come from Python and don't know Java but that's not who we should be helping.
- deleted 14y ago[deleted]
- Firehed 14y agoAre you writing this for yourself or to share? If the latter, optimize readability for people who don't understand regexes. As for commenting the end of loops - that too just improves readability, especially in long functions. If your editor doesn't show invisible characters, it can be easy to lose track if some indent is part of the 'i' loop or the 'j' loop, for example (yes, that can indicate a bigger problem, but that's not the point. I'm talking about real-world code, not idealistic academic nonsense)
- hkolek 14y agoWhy? So my grandma can read my code? That's not the way to enlightenment if you program in a professional setting.
- 6ren 14y agoIt seems that statement wasn't a hard and fast rule. ba dum tish I think everyone should do what they want, including telling others what to do, hypocritically. I prefer not to escape dots in char classes, because escapes make it less readable for me. I know this involves a more complex rule, for where one need not escape, but somehow my mind easily treats character classes as a special case region.
- ralph 14y agoIt's not necessarily your choice. [\.] in sed is a two-character character class.
- adobriyan 14y ago"After 40 Terabytes, your fingers start to hurt." --David S. Miller