0

I have a problem with validating group 2 which is the (sub)domain and extension.

My regex: /^(http:[\/]{2})([\w\d\-\_\.]+)(\/(?:[\/\w\W]+)?)?$/

Issue on: ([\w\d\-\_\.]+)

Issue: http://www.....google....com/stuff is valid, I tried doing [\w\d\-\_][(?<!\.)\.(?!\.)]+ but alas it doesn't work.

How can I make it so that if there's a . behind or a . in front of a . that the regex will return false.

https://regex101.com/r/fS9dG7/10

2 Answers 2

2

You can use a negative lookahead:

~^https?:/{2}(?!.*?\.\.)([\w\d.-]+)(/.*)?$~

RegEx Demo

btw in PHP you can also use parse_url function for parsing URLs.

(?!.*?\.\.) is a negative lookahead that will disallow 2 dots.

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

Comments

1

You can use this version of your regex:

^(http:[\/]{2})((?![^\/]*?\.{2}[^\/]*?)[\w\d\-\_\.]+)(\/(?:[\/\w]+)?)?$

The problem was with the \W (together with \w in the same character class) that matched everything, even a new line.

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.