1

I have a string which is

string a = @"\server\MainDirectory\SubDirectoryA\SubDirectoryB\SubdirectoryC\MyFile.pdf";

The SubDirectoryB will always start with a prefix of RN followed by 6 unique numbers. Now I'm trying to modify SubDirectoryB parth of the string to be replaced by a new value lets say RN012345

So the new string should look like

string b = @"\server\MainDirectory\SubDirectoryA\RN012345\SubdirectoryC\MyFile.pdf";

To achieve this I'm making use of the following helper method

public static string ReplaceAt(this string path, int index, int length, string replace)
{
   return path.Remove(index, Math.Min(length, path.Length - index)).Insert(index, replace);
}

Which works great for now.

However the orginial path will be changing in the near future so it will something like @\MainDirectory\RN012345\AnotherDirectory\MyFile.pdf. So I was wondering if there is like a regex or another feature I can use to just change the value in the path rather than providing the index which will change in the future.

13
  • What is the value in the input string you want to replace by RN012345? Is it a position in the path (i.e. 2nd sub-directory)? Commented Feb 15, 2017 at 10:26
  • There has to be some clear and consistent rule. Relying on 'RN' is going to cause you trouble - what if there's more than one subfolder in the path with 'RN'?. Is there always the same of sub-folders? Or will you know the value of SubDirectoryA at runtime? Commented Feb 15, 2017 at 10:27
  • didnt you ask almost this yesterday? I remember seeing that exact pathing... Commented Feb 15, 2017 at 10:27
  • @BugFinder it's similar but this is replacing the path and that was getting the path Commented Feb 15, 2017 at 10:27
  • @Zesty yes I will know the value of SubDirectoryA at runtime Commented Feb 15, 2017 at 10:28

3 Answers 3

2

Assuming you need to only replace those \RNxxxxxx\ where each x is a unique digit, you need to capture the 6 digits and analyze the substring inside a match evaluator.

var a = @"\server\MainDirectory\SubDirectoryA\RN012345\SubdirectoryC\MyFile.pdf";
var res = Regex.Replace(a, @"\\RN([0-9]{6})\\", m =>
        m.Groups[1].Value.Distinct().Count() == m.Groups[1].Value.Length ?
          "\\RN0123456\\" : m.Value);
// res => \server\MainDirectory\SubDirectoryA\RN0123456\SubdirectoryC\MyFile.pdf

See the C# demo

The regex is

\\RN([0-9]{6})\\

It matches a \ with \\, then matches RN, then matches and captures into Group 1 six digits (with ([0-9]{6})) and then will match a \. In the replacment part, the m.Groups[1].Value.Distinct().Count() == m.Groups[1].Value.Length checks if the number of distinct digits is the same as the number of the substring captured, and if yes, the digits are unique and the replacement occurs, else, the whole match is put back into the replacement result.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this, so if I didn't want to take into account the distinct digist I can remove m.Groups[1].Value.Distinct().Count() == m.Groups[1].Value.Length?
Then all you need is Regex.Replace(a, @"\\RN[0-9]{6}\\", "\\" + your_value + "\\")
Thanks once again!
0

Use String.Replace

string oldSubdirectoryB = "RN012345";
string newSubdirectoryB = "RN147258";

string fileNameWithPath = @"\server\MainDirectory\SubDirectoryA\RN012345\SubdirectoryC\MyFile.pdf";

fileNameWithPath = fileNameWithPath.Replace(oldSubdirectoryB, newSubdirectoryB);

4 Comments

Sorry for the confusion, the SubDirectoryB name was used in question to simiplify things. The Directory will always begin with RN followed by 6 unique numbers so this might not work
Does the application know the 6 unique numbers at runtime?
Yes it will be in in the path. So for example the entire string will contain the path and the SubDirectoryB will be RN012345 I want to replace that RN012345 with RN147258
Sorry, I don't understand the difficulty. So, can't you use myPath.Replace("RN012345", "RN147258"); Aren't both values known at runtime?
-1

You can use Regex.Replace to replace the SubDirectoryB with your required value

string a = @"\server\MainDirectory\SubDirectoryA\RN123456\SubdirectoryC\MyFile.pdf";
a = Regex.Replace(a, "RN[0-9]{6,6}","Mairaj");

Here i have replaced a string with RN followed by 6 numbers with Mairaj.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.