4 ms·
There is another benefit to using regular expressions for replacements that is not obvious, but it is a huge productivity boost: When I decide to modify code u
by robertelder 10y ago
There is another benefit to using regular expressions for replacements that is not obvious, but it is a huge productivity boost: When I decide to modify code using regexes instead of doing it by hand, my work becomes more transactional, and as a result I miss fewer errors when I realize that I need to re-do my work.
For example, let's say I have a bunch of function calls that need an extra parameter passed at the end. In vim, I might do something like
114,155s/);/, extra_parameter);/g
Of course, it might have been faster to copy and paste a bunch of times, but then I realize that I actually need 2 parameters, not one. Now I can just press 'u' do undo what I just did, escape colon, then 'up' to get the last regex which I can quickly modify:
114,155s/);/, extra_parameter, yet_another_parameter);/g
When you attempt to do and undo this stuff by hand the chances become very high that you will make subtle mistakes somewhere. In my experience, when I use regexes it is more likely that everything is right or everything is wrong in an obvious way.