Posts Tagged ‘regexp’

RegEx: Link not beginning with http://

Posted on the January 23rd, 2009 under PHP by Mauro

I had to find a regex to match not a string that contain something, but a string that not contain something, and in particular i had to check that a link entered in a form does not begin with http://. I needed to use a regex for this that work with PHP and, i have to admin, it was not so simple.
The method that i found is based on lookahead technique, but seem to work only with preg_match() and not with ereg(). Is not perfect, because it don’t match any string that contain http://, not only that begin with.
This is the regex:


(^((?!.*http\://).)*$)

This match www.ciao.com, but not http://www.ciao.com and also not www.ciao.com/http:// and any string that contain http://. It work for my needs, but is not what really wanted to achieve.
If you know a method to correct the regex, please tell me.

Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)