mihai
mihai

Reputation: 2824

Replacing some substring occurence inside a string

GIve me some advices concerning replacing substrings in a string.

At the input I have the initial string (it is a C# code always) and the NewName (MyGood) of the class.

the initial string:

using System;
using Meorfi.Core;
using Meorfi.ViewModel.Shared;
using Meorfi.ViewModel.Shared.Base;
using Meorfi.Wrapper.Base;

namespace Meorfi.ViewModel {
    public partial class ViewModel {
        public ViewModel() {
            Initialize();
        }   
     }
}

At the output It should be the following string:

using System;
using Meorfi.Core;
using Meorfi.ViewModel.Shared;
using Meorfi.ViewModel.Shared.Base;
using Meorfi.Wrapper.Base;

namespace Meorfi.ViewModel {
    public partial class MyGoodViewModel {
        public MyGoodViewModel() {
            Initialize();
        }   
     }
}

The question is: how to replace only the class name, and it's constructor.

Now, 1) using string.Replace("ViewModel", "MyGoodViewModel"); I'll have:

    using System;
    using Meorfi.Core;
    using Meorfi.MyGoodViewModel.Shared;
    using Meorfi.MyGoodViewModel.Shared.Base;
    using Meorfi.Wrapper.Base;

    namespace Meorfi.MyGoodViewModel {
        public partial class MyGoodViewModel {
            public MyGoodViewModel() {
                Initialize();
            }   
         }
    }

2) replacing the "public partial class ViewModel" with "public partial class MyGoodViewModel" and

"public ViewModel(" with "public MyGoodViewModel(" (supposing that there are more constructors), will resolve the issue,

but if there will be some more blank spaces "public _ partial __ class _ ViewModel" or "public ViewModel _("
where '
_' - are blanks, there will not be a good solution.

I want to do this in a very elegant manner. Maybe should I use Regex to do this ?

Thank you in advance.

Edit: Based on Andrew answer I solutionized the issue. (The solution follows)

private string ReplaceNames(string code, string newName, string oldName) {
         var classNamePattern = @"(?<=class\s)\s*\b" + oldName;
         var ctorPattern = @"(?<=public\s)\s*\b" + oldName;

         var updatedCode = Regex.Replace(code, classNamePattern, newName, RegexOptions.IgnoreCase);
         var match = new Regex(ctorPattern).Match(updatedCode);

         if (match.Success) {
            updatedCode = Regex.Replace(updatedCode, ctorPattern, newName, RegexOptions.IgnoreCase);
            return updatedCode;
         }
         return code;
      }

Thank you all!

Upvotes: 0

Views: 263

Answers (4)

Andrew -
Andrew -

Reputation: 36

y solution:

string patternClassName = @"(?<=class\s)\s*\S*";
string patternClassConstructor = @"(?<=public\s)\s*\S*";

private string Change(string source, string newName)
        {
            string changeClassNameResult = Regex.Replace(source, patternClassName, newName, RegexOptions.IgnoreCase);

            Match match = new Regex(patternClassName).Match(source);

            if (match.Success)
            {
                return Regex.Replace(changeClassNameResult, patternClassConstructor + match.Value, newName, RegexOptions.IgnoreCase);
            }
            else
            {
                return changeClassNameResult;
            }
        }

Upvotes: 1

Kaveh Shahbazian
Kaveh Shahbazian

Reputation: 13521

There are lots of tools for doing such things (T4 i.e) but if you want doing something done and go on, try this:

var str = Regex.Replace(@"public
partial class    ViewModel", @"public\s*partial\s*class\s*ViewModel", "public partial class MyGoodViewModel");

And str would be public partial class MyGoodViewModel.

Upvotes: 1

Liviu
Liviu

Reputation: 38

string.Replace("class ViewModel", "class MyGoodViewModel");
string.Replace("public ViewModel", "public MyGoodViewModel");

Upvotes: 0

melwil
melwil

Reputation: 2553

Try matching the space before as well, since all the packages have dots before them.

string.Replace(" ViewModel", " MyGoodViewModel");

Upvotes: 2

Related Questions