0

I want to create a regular expression for subdomain like:

In the textbox user need to enter like

abc1.test.com

or

abc1s.test.com

Note: .test.com is always required at the end.

The variable part can contain any letter, alphabets etc

I dont know anything about regex so i ask this silly question. I googled it for more than 2 hours but dont find any good example.

Please note this is not a homework.

Any help is highly appreciated.

1
  • You should probably re-phrase the qusetion title... Commented Oct 23, 2012 at 6:10

2 Answers 2

3

There are a number of useful resources on regex online, including:

I am sure other members will have better resources but these have sufficed for me in the past.

The following regex should work for you:

^((.+)(.test.com){1})$
  • the '.' is any character except new line
  • the '+' is one or more times
  • the '{1}' is exactly once
Sign up to request clarification or add additional context in comments.

1 Comment

{1} is not required. A literal repeats itself once by default. You will need to escape the . since a . is a meta character. Your regex would match abcstestlcom. A cleaner and correct regex is ^[\d\w]+\.test\.com$
1
^[\d\w]+\.test\.com$ will work for your case.

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.