Page 1 of 1
Php regex help
Posted: Wed May 26, 2010 3:14 pm
by getmizanur
elloo,
i had a php test today and had to find what was wrong with the following regex
preg_match('/^(?:ftp://)?([^/]+)@i');
my answer
preg_match('/^(?:ftp:\/\/)?([^/]+@/i');
can anyone tell me if i'm correct or not.
and just for understanding, can anyone explain to me what this means (?:ftp://)?([^/]+)
thanks in advance
Re: Php regex help
Posted: Thu May 27, 2010 11:20 am
by ridgerunner
getmizanur wrote:elloo,
i had a php test today and had to find what was wrong with the following regex
preg_match('/^(?:ftp://)?([^/]+)@i');
my answer
preg_match('/^(?:ftp:\/\/)?([^/]+@/i');
can anyone tell me if i'm correct or not.
and just for understanding, can anyone explain to me what this means (?:ftp://)?([^/]+)
thanks in advance
I think the solution they were looking for was this (changing only the firsr regex delim char from / to @):
Code: Select all
preg_match('@^(?:ftp://)?([^/]+)@i');
Your solution was almost correct, but you forgot to escape the forward slash inside the character class (and I don't think they actually wanted to match the @ at-sign - it was supposed to be the regex delimiter).
As to what does '(?:ftp://)?([^/]+)' mean? Well, it means:
Optionally match the literal text 'ftp://' and then match one or more non-forward slash characters capturing them into group one.
This is pretty basic stuff. Check out
http://www.regular-expressions.info/.