开发者

Visual Studio 2008 find replace regex

开发者 https://www.devze.com 2023-02-10 00:51 出处:网络
Is there any ways I can do this? Find: if (value) _select.Add(X); else _select.Remove(X); and replace with:

Is there any ways I can do this?

Find:

if (value) _select.Add(X); else _select.Remove(X);

and replace with:

addToSelect(value1, _select1, X);

where X is a variable that I don开发者_运维技巧't know and I want to copy it in the replace.

Thank you in advance!


Use 'find and replace' with the option 'use regular expressions' with this regex :

if:b@\(:b@value:b@\):b@\n@:b@\{@:b@\n@:b@_select:b@\.:b@Add:b@\({.#}\):b@;:b@\n@:b@\}@:b@\n@:b@else:b@\n@:b@\{@:b@\n@:b@_select:b@\.:b@Remove:b@\(\1\):b@;:b@\n*:b*\}*

and replace it with this :

addToSelect(value1, _select1, \1);

Thanks to the :b@ and the \n@ it will also catch version with spaces :

if (  value  ) _select.  Add(  X  );  else   _select.Remove(  X  ) ;

or versions on multiple lines :

if (value) 
  _select.Add(X); 
else 
  _select.Remove(X);

One warning however, X appears two times in your Find example. The regex will not check if the second X is equal to the first X. The second X is just ignored and the X in

addToSelect(value1, _select1, \1);

will always be the first X.

EDIT : updated the regex to check that the two X's are always equal.

EDIT 2 : updated the regex to also catch versions with { and } :

if (value) 
{
  _select.Add(X); 
}
else 
{
  _select.Remove(X);
}


Do not have VS2008 but in VS2010 I would try this (not sure whether this option is available in 2008):

1) Ctrl+Shift+H or Edit -> Find and replace -> Replace in files
2) In Find options panel select use regular expressions

I'm not a master in regular expressions so I'm not sure whether it will replace with regex or only search with regex while replacing with plain text.

0

精彩评论

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