The rules you need seem to be the following:
- Folder name = last string preceding a '/' character but not containing a '/' character
- content name = last string not containing a '/' character until (but not including) a '_' or '.' character
- folderpath = same as folder name except it can contain a '/' character
Assuming the rules above - you probably want this code:
string value = @"/webdav/MyPublication/Building%20Blocks/folder0/folder1/content_1.xml";
var foldernameMatch = Regex.Match(value, @"([^/]+)/[^/]+$");
var contentnameMatch = Regex.Match(value, @"([^/_\.]+)[_\.][^/]*$");
var folderpathMatch = Regex.Match(value, @"(.*/)[^/]*$");
if (foldernameMatch.Success && contentnameMatch.Success && folderpathMatch.Success)
{
var foldername = foldernameMatch.Groups[1].Value;
var contentname = contentnameMatch.Groups[1].Value;
var folderpath = folderpathMatch.Groups[1].Value;
}
else
{
// handle bad input
}
Note that you can also combine these to become one large regex, although it can be more cumbersome to follow (if it weren't already):
var matches = Regex.Match(value, @"(.*/)([^/]+)/([^/_\.]+)[_\.][^/]*$");
if (matches.Success)
{
var foldername = matches.Groups[2].Value;
var contentname = matches.Groups[3].Value;
var folderpath = matches.Groups[1].Value + foldername + "/";
}
else
{
// handle bad input
}