Regex to find URLs unless they include a list of strings

Viewed 32

I have this the following expression which is working well for my needs:

(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.(com|net|info)\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

It does a pretty good job of finding URLs which are hardcoded in a large codebase. Obviously, it has some false positives for things like Using System.net, but some of the valid URLs don't include http(s):// unfortunately.

So now I want to be able to exclude certain things, including system.net (false positives) and google.com (common urls in comments).

How can I do this?

Ideally the list would just include google and system. Something like (!google|!system).

1 Answers

Received this answer on Reddit less than an hour after posting.

you can use a negative lookahead for this.

\b(?!google\.com|system.net)

\b is there to make sure it starts at a word boundary (like after a space or a dot there)

https://regex101.com/r/pwj3Xn/1

Credit to u/Kompaan86/

Related