Simple Search and Replace use of Regular Expression
So, I am adapting some code I found online to suit my needs. However, my set_pixel function has two more parameters. Since there are lots of calls to this function even doing a quick paste over would be very tedious. So, I thought this would be a good time for me to learn some simple regular expressions.
So, I have calls of this type:
set_pixel(arg1, arg2);
which I want to change to something like:
set_pixel(arg1开发者_JAVA技巧, arg2, arg3, arg4);
Note: arg1 and and 2 should be preserved, whereas arg3 and arg4 are most of the time the same.
How can I achieve this?
/set_pixel\(([^,)]*),([^,)]*)\)/
replace with
set_pixel(\1,\2,arg3,arg4)
Depending on the language and the situation you might be better served though with some sort of refactoring tool, default parameters, overloading etc.
You can use something like this:
s/set_pixel\(([a-zA-Z0-9_]*), ([a-zA-Z0-9_]*)\);/set_pixel\($1, $2, arg3, arg4\);/g
with your favorite regular expression toy. You will want to tweak the character classes based on the inputs you expect to set_pixel.
精彩评论