Reputation: 583
I need a regex to use in a C# App with the follow structure:
Valid Example:
domain1.com;domain2.org;subdomain.domain.net
How can i do that with a single Regex?
Upvotes: 1
Views: 2195
Reputation: 1280
Using Mr. Disappointment's suggestion, I don't know what all the Uri.IsWellFormedUriString method does, but, in theory, if you want to perform both steps (separate and validate) in one, I would think you could use LINQ to do something like this:
(editted this to use Uri.CheckHostName instead of Uri.IsWellFormedUriString)
string src = "domain1.net;domain2.net; ...";
string[] domains = src.Split(';').Where(x => Uri.CheckHostName(x) != UriHostNameType.Unknown).ToArray();
Upvotes: 0
Reputation: 7187
You can use [^;]+ but C# has a split function which will work well for this(if you can avoid regex I would do so)
http://coders-project.blogspot.com/2010/05/split-function-in-c.html
Upvotes: 0
Reputation: 32333
Just:
var domains = example.Split(";".ToCharArray(),
StringSplitoptions.RemoveEmptyEntries);
Upvotes: 3
Reputation: 45083
Given my aversion to regex in general, I am compelled to post an alternative (banking on the fact that you're going to need the individual domain representations as separate entities at some point anyway):
string[] domains = delimitedDomains.Split(';');
Where delimitedDomains
is a string of domains in the format mentioned and the result being an array of each individual domain entry found.
It may well be that there are more cases to your scenario than precisely detailed, however, if this is the extent of your goal and the input then this should suffice quite well.
Upvotes: 3
Reputation: 4699
Could try
^([^\.;]+\.[^\.;]+;)*$
Depending on if you want specific domain names you may have to alter it
Upvotes: 0