Regular expression which excludes all kinds of URL

89 Views Asked by At

I could find various examples of regular expression regarding http / protocols and urls but still I could not find the exact regular expression which can avoid all kinds of url or protocols.

My aim is to not allow any kind of url in a text box ( http://example.com, https://example.com, www.example.com , http://example.in etc....) . I have tried multiple regular expression but cud not get the proper one...

I have to just add this regular expression as a property which will be than executed by already created java class of regular expression.

Gaurav

2

There are 2 best solutions below

0
On

@AvinashRaj I have put the regex in negative lookahead but that seems not to be working ^(?!.*/(?:(https?:\/\/)|\1?www\.)\w+\.\w{2,5}/).*

You just need to remove the regex delimiters.

^(?!.*(?:(https?:\/\/)|\1?www\.)\w+\.\w{2,5}).*

DEMO

0
On

From a bit of research I realised that it is hard to do a foolproof one for all the possible URLs. This website has regex which matches many website URLs. What you can do is use the complement of that regex.