简体   繁体   中英

Visual Studio 2010: pattern based search replace other than regex?

I've taken over a project that is full of code like this:

if (aTraceUserids[t].Trim().ToUpper() == Userid().Trim().ToUpper())
{
    // ...
}

What is - using tool-assisted expression formulation - a good way to do a search replace into something like this on a case by case base:

if (aTraceUserids[t].Equals(Userid(), StringComparison.InvariantCultureIgnoreCase))
{
    // ...
}

Edit (thanks Dave for making me think on this further):

I know this should be possible with regular expressions , but those are hard to get right and document, so I wonder about tool assisted ways that help me both phrase the expressions and execute them.

Ideally I'm looking for a pattern based search/replace tool that allows me to

  • enter the search/replace patterns
  • enter the patterns for the files and directory names to match
  • visually assists me with the search/replace matches, and allows me to post-edit each occurrence

I don't care much which platform as these kinds of search/replace actions will likely apply to other big code bases as well.

So: any solution based on *nix, Windows or web are fine. CygWin and/or WINE based solutions are fine too. (That's why I removed the VS2010 tag and added some platform tags).

Since this was originally tagged with 'Visual Studio', Visual Studio itself can do regular-expression based find/replace, and the standard 'Find and Replace' dialog will let you pick and choose by hitting 'Find Next', 'Replace' or 'Replace All' as you choose.

For example, I recently changed an API from;

Log.Error(string message, bool someotherArg);

to

Log.Error(string message);

And easily used Visual Studio to replace all usage throughout my codebase of this modified API like so;

Find what; Log.Error({.*}, true);

Replace with: Log.Error(\\1);

The backquote in the replace line \\1 puts the grouped regex contained by {...} into that spot in the replace.

Handy, and built-in. Works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM