'Regex in Visual Studio Comunity

I'm trying to replace a code using Regex in visual studio community, but I'm not getting it.

ZeroMemory(&ab,15);

ZeroMemory(&abc,30);

ZeroMemory(&tickt,sizeof(tickt));

To replace

memset(&ab,0,15);

memset(&abc,0,30);

memset(&tickt,0,sizeof(tickt));

I need to replace zeromemory with memset and the first one with ,0, thank you all in advance



Solution 1:[1]

Clearly you are asking about the "Find and Replace" feature in Visual Studio.

As a starting point, something like this should do what you want:

  • From: ZeroMemory\s*\((.*),
  • To: memset($1, 0,

Because parentheses are used in regular expressions for matching groups, you need to escape them (\() if you want to match an actual parenthesis.

Then we use (.*) to create a group that captures whatever your first parameter is. Note that this is very naive. If you have a non-trivial expression for that argument (e.g. a function call where a comma appears anywhere) this will likely fail.

So that matches the stuff you want, and then for the replacement you can use $1 to substitute the first matching group (in which we captured your first argument).

The only extra detail I added was a \s* between the function identifier and the parenthesis. This matches optional whitespace characters, so it will still match even if you have stuff like:

ZeroMemory   (foo, bar);

I recommend you read the documentation to familiarize yourself with regular expressions in Visual Studio.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paddy