I have such types of file paths:
\\server\folder\folder1\folder3\someFile.txt
,\\otherServer\folder123\folder1\folder3\someFile.txt
\\serv\fold\folder3\folder4\someFile.txt
I need to remove the first two segments of this path to make them as follows:
folder1\folder3\someFile.txt
,folder1\folder3\someFile.txt
folder3\folder4\someFile.txt
I'm doing it with c# and Regex.Replace
but need a pattern.
Thanks.
CodePudding user response:
It seems, that you work files' paths, and that's why I suggest using Path
not Regex
:
using System.IO;
...
string source = @"\\serv\fold\folder3\folder4\someFile.txt";
var result = Path.IsPathRooted(source)
? source.Substring(Path.GetPathRoot(source).Length 1)
: source;
If you insist on regular expressions then
string result = Regex.Replace(source, @"^\\\\[^\\] \\[^\\] \\", "");
CodePudding user response:
You can use the following regular expression pattern to remove the first two segments: ^\\[^\\] \\[^\\] \\
you can use Regex.Replace in C# example:
string input = "\\\\server\\folder\\folder1\\folder3\\filename.txt";
string pattern = "^\\\\[^\\\\] \\\\[^\\\\] \\\\";
string replacement = "";
var result = Regex.Replace(input, pattern, replacement);
output:
folder1\folder3\filename.txt