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.

2
  • I think your problem is that - hasn't been allowed... that said, my RegEx isn't great. Commented Apr 17, 2013 at 11:26
  • Why reinvent the wheel, poorly? There's a few functions like Uri.CheckHostName() (as supposed here) and a hideous regex is shown here. Commented Apr 17, 2013 at 11:30

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.