tags:

views:

42

answers:

2

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, 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?

A: 

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.

WhirlWind
+1  A: 
/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.

Tgr
I am using CodeBlocks built in Search and Replace with the Regular Expression checkbox checked, but it says I don't get any matches...
nunos
Leave out the leading and trailing slash, they are common regex delimiters in languages like JavaScript and others, but you don't need them in a development tool like CodeBlocks.
Tim Pietzcker