1

My application accepts the following type of URL

Example:

ABa12C.constant.com or

http://ABa12C.constant.com or

https://ABa12C.constant.com

  1. (Optional)Starts with http:// or https:// or nothing.
  2. Should not start with integer(1aAb4.constant.com is invalid).
  3. In place of com it can be any like(in,uar,sr etc) but no integers.

My regular expression: [http://a-zA-Z0-9]{1,20}.constant.[a-zA-Z]{1,5}

But how to make http:// optional and can be used only at beginning and cannot be integer at beginning.

7
  • 3
    Looks like you misuse character classes. What are you trying to do with [http://a-zA-Z0-9]{1,20}? Try ^(https?:\/\/)?[^0-9][a-zA-Z0-9]{0,19}\.constant\.[a-zA-Z]{1,5}$ Commented Jul 18, 2016 at 9:28
  • ^ means checks at the start of string and ? means optional. But [^0-9] makes integer compulsory but i don't want integer at beginning. Commented Jul 18, 2016 at 9:50
  • 1
    You have not answered the question: what are you trying to achieve with [http://a-zA-Z0-9]{1,20}? There is no any length restriction requirement listed in your question. Note that [^0-9] makes a non-digit compulsory. Commented Jul 18, 2016 at 9:57
  • Yes its working. So here ^ acting as start of string and also as exclude [0-9]. I mean does ^ character have two meanings in different locations. Commented Jul 18, 2016 at 11:27
  • 1
    [^0-9] matches any character other than a digit. It matches ~, \n, *, a, _, space. A lot of things. Commented Jul 18, 2016 at 11:42

2 Answers 2

1

Your pattern has no anchors, and the initial subpattern is a character class [http://a-zA-Z0-9]{1,20} that matches 1 to 20 chars from the class, either h or t, p, :, /, a-z, A-Z, 0-9 while you need to match http:// as a sequence.

I suggest

^(https?:\/\/)?[a-zA-Z][a-zA-Z0-9]{0,19}\.constant\.[a-zA-Z]{1,5}$

See the regex demo

Explanation:

  • ^ - start of string
  • (https?:\/\/)? - an optional sequence of http:// or https://
  • [a-zA-Z] - an ASCII letter
  • [a-zA-Z0-9]{0,19} - 0 to 19 alphanumeric characters (the length restriction can be adjusted by you)
  • \.constant\. - a constant substring .constant.
  • [a-zA-Z]{1,5} - 1 to 5 ASCII letters
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?).
0

The following regex does the trick:

(?:https?:\/\/)?[^0-9]\S*\.[^0-9]\S* demo

Note that a non-capturing group (?:) is used for the protocol.

1 Comment

It does not matter much if the group is capturing or not, but the [^0-9] will allow any symbol other than digits, while it seems OP needs only alphanumeric values in the input string.

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.