Esteban Lopez
Esteban Lopez

Reputation: 583

Regex : domains separated by semicolon

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

Answers (6)

Steven
Steven

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

Spooks
Spooks

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

Oleks
Oleks

Reputation: 32333

Just:

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

Upvotes: 3

Grant Thomas
Grant Thomas

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

TimCodes.NET
TimCodes.NET

Reputation: 4699

Could try

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

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

Upvotes: 0

grizzly
grizzly

Reputation: 1156

Use this one:

[^\;]+

Upvotes: 0

Related Questions