开发者

Searching for particular word in files

开发者 https://www.devze.com 2023-01-19 12:01 出处:网络
We have a requirement to replace word \'ABC\'(dummied) with \'XYZ\'(dummied) in all the files. We have hundreds of aspx/ascx/cs/xml/xsl files which might contain the word.

We have a requirement to replace word 'ABC'(dummied) with 'XYZ'(dummied) in all the files. We have hundreds of aspx/ascx/cs/xml/xsl files which might contain the word.

The requirement is

  1. Do not replace word if it is part of NameSpace(eg ABC.A1.A2 开发者_开发技巧should not be replaced) or variable names(var abc = "") or tags (abc:control).
  2. The word should be replaced only if it is a whole word( In my cs file, if I have a variable name same as the word i am looking for, it should not be replaced . If I have a string literal eg var str = " ABC is good product". It should be replaced in this case

This will be 1 time activity. How do I parse entire file for the given word and replace it if condition is satisfied? Planning to use Regex.

Any other ideas is really appreciated. Does a tool already exists that does this? I have tried to google but could not find such a tool.


Tools like PowerGREP are always helpful. Together with RegexBuddy you have a powerful toolset which helps to learn regular expressions and master every regex challenge.


If this is truly a one-time activity, I'd vote for simply using Visual Studio Search/Replace, even if it's a bunch of solutions. Why reinvent the wheel, especially since, realistically, you are much more likely to not do it correctly the first time, and have to undo hundreds of changes manually?

If you use source control (you do use source control, right?), I would highly recommend doing a diff on the files before you check them in, no matter what solution you use.


Investigate if you can translate you requirements into regular expressions. Probably one filetype at a time.
Then, don' write a program but use an existing tool (SED).

But some of your rules seem very complex.

0

精彩评论

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

关注公众号