1

can't find clean way to do this - just want to replace the two matched strings - but every example is pretty hacky (use substrings to split up string based on matches??), isn't there a clean way to just replace the two matched groups in a string?? thanks

var inLine = "Project(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Common\", \"Libraries\\Common\\Common.csproj\", \"{91197577-34B9-4D46-B3FE-A8589D4380B1}\"";
var regex = new Regex("Project\\(\\\"{.*}\\\"\\) = \"(?<projectName>\\S*)\", \"(?<relativePath>\\S*)\", \"{.*}\"");
var newProjectName = "Blah";
var newRelativePath = "..\Core\Libraries\Blah\Blah.csproj";
var match = regex.Match(inLine);
if (match.Success)
{
    var projectName = match.Groups[1].Value;
    var relativePath = match.Groups[2].Value;
    var replaced = regex.Replace(inLine, m =>
       {
          // ??????????
          return ""; 
       });
    // want replaced to be:
    // Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blah", "..\Core\Libraries\Blah\Blah.csproj", "{91197577-34B9-4D46-B3FE-A8589D4380B1}"
}

Edited post: Expected behavior is input string:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Libraries\Common\Common.csproj", "{91197577-34B9-4D46-B3FE-A8589D4380B1}"

Want to replace Common and Libraries\Common\Common.csproj with 2 other strings like Blah and ..\Core\Libraries\Blah\Blah.csproj so new string value (replaced) would be:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blah", "..\Core\Libraries\Blah\Blah.csproj", "{91197577-34B9-4D46-B3FE-A8589D4380B1}"

dan
  • 801
  • 15
  • 41
  • Add a Group Name to each part of the Regex. So then you can use Named Matched Subexpressions from link https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions – jdweng May 15 '20 at 17:10
  • In order to answer your question as you need, please explain what you need to get in the end? A `projectName` variable with the project name, `relativePath` with the relative path and what should `replaced` look like? – Wiktor Stribiżew May 15 '20 at 17:31
  • I think you need to replace something in the original string, but also initialize a couple of variables, please see https://ideone.com/zo9HUs – Wiktor Stribiżew May 15 '20 at 17:45
  • @jdweng thanks - I already did add the group name to each part (see ? and ?) and I saw that msdn doc, but not clear how to actually replace those matches in original string with something else. updated original post with what I am wanting.thank you. – dan May 15 '20 at 19:32
  • @WiktorStribiżew - thanks - I updated original post w/ output I am wanting. Saw your example, thanks, but didn't really get output I wanted and not sure how to do it. any other ideas?? thank you. – dan May 15 '20 at 19:34
  • 1
    Then it sounds like it is as easy as https://ideone.com/PHJeQL – Wiktor Stribiżew May 15 '20 at 20:00
  • thanks @WiktorStribiżew the replace worked but just a match doesn't work with your new pattern.. do i need two different patterns for this? because I need to change a bunch of these so i have a lookup, why i edited code snippet in my post to include vars for the new values.... ? thanks again! – dan May 15 '20 at 20:20
  • See https://ideone.com/laZgIu – Wiktor Stribiżew May 15 '20 at 20:27

1 Answers1

0

I Googled a bit and found something interesting here

I adapted their solution a bit so it would fit the criteria. It appears to work rather nicely. Although I'm not sure if it is better than the remove insert method.

Here is my code:

var name = "Blah";
var location = "..\\Core\\Libraries\\Blah\\Blah.csproj";
var result = Regex.Replace(INPUT, "^(.*\")(\\w+)(\".*\")([\\w\\\\\\.]+)(\".*)$", m =>
    {
        return string.Join("",
                m.Groups.OfType<Group>().Select((g, i) =>
                {
                    switch (i)
                    {
                        case 1:
                            return g.Value;
                        case 2:
                            return name;
                        case 3:
                            return g.Value;
                        case 4:
                            return location;
                        default:
                            return g.Value;
                    }
                }).Skip(1).ToArray());
    });

Here is the fully working code on ideone

Rubeste
  • 75
  • 1
  • 8