0

My case:

  1. String is a Valid URL and does not contain fbdcdn.net somewhere in the String
  2. String can also be simply N/A

So far i have the current regex which will successful check if a URL ist Valid and can also be simply N/A

Regex

http(?:s)?:\/\/(?:www\.)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:\/?#[\]@%!\$&'\(\)\*\+;=.]+$|(?:N\/A)

Should Work

https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1200px-Google_2015_logo.svg.png

Should not Work

https://scontent-frt3-1.xx.fbdcdn.net/v/t1.0-9/abcde.jpg?_nc_cat=108&_nc_ht=scontent-frt3-1.xx

Since i cant get this working i would appreciate some help or explanation what should I do next.

Thanks in advance

sHamann
  • 567
  • 2
  • 6
  • 29

1 Answers1

2

If your input string is just the URL (or N/A), you can do this fairly easily with a negative lookahead. This will fail the match if fbdcdn.net appears anywhere in the url.

(?!^.*?fbdcdn.net.*?$)^(?:http(?:s)?:\/\/(?:www\.)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:\/?#[\]@%!\$&'\(\)\*\+;=.]+$|(?:N\/A))$

Essentially, this is the same regex you supplied in your question, with minor modification: (?!^.*?fbdcdn.net.*?$)^...$

(?!            // Start negative lookahead (fail entire match if lookahead matches)
   ^           // Anchor to the start of the string
   .*?         // look for any characters (lazily)
   fbdcdn.net  // then match our forbidden string
   .*?         // then look for any more characters (lazily)
$)             // before we find the end of our string, end lookahead
^              // anchor the pattern to the start of the string
...            // your URL matching pattern
$              // anchor the pattern to the end of the string
Zaelin Goodman
  • 898
  • 2
  • 11