Search code examples
regexstringperlpcre

How to ignore all after domain zone except symbol "/"


I have regex for match domain with username:

/(?:https:\/\/)?(?:http:\/\/)?(?:www\.)?(?:facebook)\.com\/(\w+(?:\.\w+)*)$/

This regex match example URLs:

facebook.com/username
www.facebook.com/username
http://facebook.com/username
http://www.facebook.com/username
https://facebook.com/username
https://www.facebook.com/username

How change this regex for match only URLs with domain zone and non required symbol /:

facebook.com
facebook.com/
.....................
https://facebook.com/
https://www.facebook.com

Solution

  • You are only wanting to match strings that contain the domain only, then you can use something like this:

    ^(?:https?:\/\/)?(?:www\.)?facebook\.com\/?$
    

    This will match regardless if it has the protocol (http(s):\/\/) and regardless if it contains www..

    See it live

    Breaking down the regular expression, ^(?:https?:\/\/)?(?:www\.)?facebook\.com\/?$

    • ^ start of string
    • (?:https?:\/\/)? a non-capturing group that will match the protocol https?:\/\/, zero or one time ? (optional)
    • (?:www\.)? non-capturing group that will match on www., zero or one time ? (optional)
    • facebook\.com will match the domain
    • \/? will match an optional ? forward slash \/
    • $ end of string (emphasis added) - this is what allows this to work with your requirements as this will not allow anything to match after the optional forward slash in the prior bullet.