donpisci
donpisci

Reputation: 349

Regular Expression for Subdomain

I was hoping you could help me with a regular expression I'm having trouble writing for my subdomain.

The basic format of my domain will be something like this:

http://xxxxxxx-md.mywebsite.com

The http:// is optional and the xxxxxxxx is not a set length and hasn't any restrictions (yet!). The only thing I need to check for is the -md which will always be at the end of the subdomain.

At the moment, this is what I've got but I don't think it's working properly.

(http:\/\/)?[a-zA-Z0-9]*|(\W)-md.mywebsite.com(\W)

Can you take a look and advise please?

Thanks!

Upvotes: 0

Views: 1375

Answers (1)

Jerry
Jerry

Reputation: 71578

Your current regex will match either:

(http:\/\/)?[a-zA-Z0-9]*

OR

(\W)-md.mywebsite.com(\W)

Which is not what you probably want. Use a group to restrict the OR:

(http:\/\/)?(?:[a-zA-Z0-9]*|(\W))-md.mywebsite.com

And you want a single non-alphanumeric, digit, underscore in the xxxxx part? If you want to allow more, you'd use:

(http:\/\/)?(?:[a-zA-Z0-9]*|\W*)-md.mywebsite.com

Or perhaps:

(http:\/\/)?(?:[a-zA-Z0-9]|\W)*-md.mywebsite.com

Oh and I removed the (\W) part because it doesn't seem useful, unless you had a purpose for it.

Upvotes: 1

Related Questions