Reputation: 14088
I'm writing a custom URI parser (which extends GenericUriParser) in order to allow my program to deal with unix/linux-style smb://
and nfs://
URIs.
In order to do this, I need to override the GetComponents method which is responsible for filling in the various properties of the Uri
object (e.g. host, path, absolute URI ...). This is where I get stuck. I have absolutely no idea on how to 'split' the input string by meaning.
Examples of valid input (smb only, nfs is even more complicated):
smb://hostname/public/example.txt
smb://username:password@hostname/public/example.txt
smb://10.0.0.1/public
Where do I even begin to solve this problem?
Upvotes: 0
Views: 436
Reputation: 27585
I suggest you to use Regex
. For the given url in question, the regex pattern could be something like this:
var pattern =
"^smb:\/\/((?<user>[\w-]+):(?<pwd>[\w-]+)@)?(?<host>[a-z0-9\.-]+)(?<path>.+)$";
You can match a url with pattern and extract parts by name. E.g.
var regex = new Regex(pattern);
var match = regex.Match(url);
if(match.Success) {
// the url is smb
var username = match.Groups["user"].Value;
var password = match.Groups["pwd"].Value;
var hostname = match.Groups["host"].Value;
var path = match.Groups["path"].Value;
}
For the given urls in question, we will have these variables:
// smb://hostname/public/example.txt
username: string.Empty
password: string.Empty
hostname: hostname
path: /public/example.txt
// smb://username:password@hostname/public/example.txt
username: username
password: password
hostname: hostname
path: /public/example.txt
// smb://10.0.0.1/public
username: string.Empty
password: string.Empty
hostname: 10.0.0.1
path: /public
The given pattern is a simple and demo pattern. So, if you want to go this way, you should create complex and complete and correct patterns to code get worked correctly. Let me know if you have any question or problem.
Upvotes: 2