0

I am new developer and don't have much exposure on Regular Expression. Today I assigned to fix a bug using regex but after lots of effort I am unable to find the error.

Here is my requirement.

My code is:

string regex = "^([A-Za-z0-9\\-]+|[A-Za-z0-9]{1,3}\\.[A-Za-z0-9]{1,3}\\.[A-Za-z0-9]    {1,3}\\.[A-Za-z0-9]{1,3}):([0-9]{1,5}|\\*)$";

Regex _hostEndPointRegex = new Regex(regex);

bool isTrue = _hostEndPointRegex.IsMatch(textBox1.Text);

It's throwing an error for the domain name like "nikhil-dev.in.abc.ni:8080".

I am not sure where the problem is.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
agnikhil82
  • 25
  • 1
  • 6
  • I think your problem is that `-` hasn't been allowed... that said, my RegEx isn't great. – Danny Beckett Apr 17 '13 at 11:26
  • Why reinvent the wheel, poorly? There's a few functions like [`Uri.CheckHostName()`](http://msdn.microsoft.com/en-us/library/system.uri.checkhostname.aspx) (as supposed [here](http://stackoverflow.com/questions/967516/best-way-to-determine-if-a-domain-name-would-be-a-valid-in-a-hosts-file)) and a hideous regex is shown [here](http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address). – CodeCaster Apr 17 '13 at 11:30

1 Answers1

2

Your regex is a bit redundant in that you or in some stuff that is already included in the other or block.

I just simplified what you had to

(?:[A-Za-z0-9-]+\.)+[A-Za-z0-9]{1,3}:\d{1,5}

and it works just fine...

I'm not sure why you had \ in the allowed characters as I am pretty sure \ is not allowed in a host name.

Your problem is that your or | breaks things up like this...

[A-Za-z0-9\\-]+

or

[A-Za-z0-9]{1,3}\\.[A-Za-z0-9]{1,3}\\.[A-Za-z0-9]{1,3}\\.[A-Za-z0-9]{1,3}

or

\*

Which as the commentor said was not including "-" in the 2nd block.

So perhaps you intended

^((?:[A-Za-z0-9\\-]+|[A-Za-z0-9]{1,3})\.[A-Za-z0-9]{1,3}\.[A-Za-z0-9]{1,3}\.[A-Za-z0-9]{1,3}):([0-9]{1,5}|\*)$

However the first to two or'ed items would be redundant as + includes {1-3}.

ie. [A-Za-z0-9\-]+ would also match anything that this matches [A-Za-z0-9]{1,3}

You can use this tool to help test your Regex: http://regexpal.com/

Personally I think every developer should have regexbuddy

The regex above although it works will allow non-valid host names.

it should be modified to not allow punctuation in the first character.

So it should be modified to look like this.

(?:[A-Za-z0-9][A-Za-z0-9-]+\.)(?:[A-Za-z0-9-]+\.)+[A-Za-z0-9]{1,3}:\d{1,5}

Also in theory the host isn't allowed to end in a hyphen.

it is all so complicated I would use the regex only to capture the parts and then use Uri.CheckHostName to actually check the Uri is valid.

Or you can just use the regex suggested by CodeCaster

John Sobolewski
  • 4,512
  • 1
  • 20
  • 26