Since you find it helpful, I am posting my suggestion.
The regular expression can be:
^(?!www\.|(?:http|ftp)s?://|[A-Za-z]:\\|//).*
See demo
Note that it is becoming more and more unreadable if you start adding exclusions or more alternatives. Thus, perhaps, use VERBOSE mode (declared with re.X
):
import re
p = re.compile(r"""^ # At the start of the string, ...
(?! # check if next characters are not...
www\. # URLs starting with www.
|
(?:http|ftp)s?:// # URLs starting with http, https, ftp, ftps
|
[A-Za-z]:\\ # Local full paths starting with [drive_letter]:\
|
// # UNC locations starting with //
) # End of look-ahead check
.* # Martch up to the end of string""", re.X)
print(p.search("./about.html")); # => There is a match
print(p.search("//dub-server1/mynode")); # => No match
See IDEONE demo
The other Washington Guedes's regexes
^([a-z0-9]*:|.{0})\/\/.*$
- matches
^
- beginning of the string
([a-z0-9]*:|.{0})
- 2 alternatives:
[a-z0-9]*:
- 0 or more letters or digits followed with :
.{0}
- an empty string
\/\/.*
- //
and 0 or more characters other than newline (note you do not need to escape /
in Python)
$
- end of string
So, you can rewrite it as ^(?:[a-z0-9]*:)?//.*$
. he i
flag should be used with this regex.
^[^\/]+\/[^\/].*$|^\/[^\/].*$
- is not optimal and has 2 alternatives
Alternative 1:
^
- start of string
[^\/]+
- 1 or more characters other than /
\/
- Literal /
[^\/].*$
- a character other than /
followed by any 0 or more characters other than a newline
Alternative 2:
^
- start of string
\/
- Literal /
[^\/].*$
- a symbol other than /
followed by any 0 or more characters other than a newline up to the end of string.
It is clear that the whole regex can be shortened to ^[^/]*/[^/].*$
. The i
option can safely be removed from the regex flags.