I need to capture all the urls in a paragraph apart from the urls from a specific domain/ sub domain.For example in the below paragraph I need to capture all the urls apart from example.com
"This is a paragraph name.url.com it contains random urls name-dev.url.com name-qa.url.com www.example.com test.example.com http://TestCaSeSensetivEUrl.com http://www.test.com https://www.example.com test.com"
Urls I need to capture
Urls I don't need to capture as below
test.example.com
I have tried the below regex using negative look behind method, but it's not working as I need.
/(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([a-z0-9]+(?<!example)[\-\.]{1}[a-z0-9+]+(?<!example)\.[a-z]{2,5})/gi
This should be sufficient for your use case:
/(?<!\S)(?:https?:\/\/)?(?:(?:(?!example)\w+[.-])+[a-z]{2,11})(?!\S)/gi
See https://regex101.com/r/NdOxKt/1 for a demonstration of the regex at work. Below is a rough explanation of what the regex is doing:
(?<!\S) essentially splits the string into segments on space characters, including whitespace and newlines?: syntax makes each set of parenthesis it is in a non-capture group, saving memory on the machine where it is ran and speeding up your execution time(?:https?:\/\/)? optionally matches both http and https for URLs without matching the invalid characters : and / anywhere else in the URL(?:(?!example)\w+[.-])+ looks for one or more words that do not match example, followed by either a hyphen or a period[a-z]{2,11} matches the final domain extension, i.e. com, org, or enterprisesthis could be a solution
^(https?:\/\/)?(?!(?:www\.)?google\.*)([\da-zA-Z.-]+)\.([a-zA-Z\.]{2,6})([\/\w .-]*)*\/?$
for example here google is excluded from being captured