开发者

Regex : domains separated by semicolon

I开发者_如何学Python need a regex to use in a C# App with the follow structure:

  • domains separated by semicolon

Valid Example:

domain1.com;domain2.org;subdomain.domain.net

How can i do that with a single Regex?


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.


Just:

var domains = example.Split(";".ToCharArray(), 
                  StringSplitoptions.RemoveEmptyEntries);


Use this one:

[^\;]+


Could try

^([^\.;]+\.[^\.;]+;)*$

Depending on if you want specific domain names you may have to alter it


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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜