Please can try this one
https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)?(?![^<]*(?:<\/\w+>|\/?>))
https?
: This part matches the protocol of the URL. The s is optional, so it matches both http and https.
:\/\/
: This matches the colon and two forward slashes that are part of the protocol.
(?:w{1,3}\.)?
: This matches the optional www subdomain, if it exists. The (?: ) creates a non-capturing group, and the {1,3} quantifier matches between 1 and 3 instances of the letter "w".
[^\s.]+
: This matches the domain name, which consists of one or more characters that are not whitespace or a dot.
(?:\.[a-z]+)*
: This matches any optional subdomains that come before the domain, separated by dots. The (?: ) creates another non-capturing group, and the * quantifier matches zero or more instances of this group. The [a-z]+ matches one or more lowercase letters.
(?::\d+)?
: This matches an optional port number that might come after the domain, preceded by a colon. The (?: ) creates another non-capturing group, and the ? quantifier makes it optional. The : matches the colon, and \d+ matches one or more digits.
(?![^<]*(?:<\/\w+>|\/?>))
: This is a negative lookahead that ensures that the URL is not part of an HTML tag. It checks if there are no characters before the URL that match the pattern (?:<\/\w+>|\/?>)
, which matches any closing HTML tag or an empty tag.