开发者

Simple Search and Replace use of Regular Expression

开发者 https://www.devze.com 2022-12-30 00:40 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号