Jump to content

Php regex help


getmizanur

Recommended Posts

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://)?([^/]+)

Link to comment
https://forums.phpfreaks.com/topic/203012-php-regex-help/
Share on other sites

It's very difficult to say without knowing what it's supposed to match, but are you sure that's the answer you put? Because it is slightly incorrect. You are missing a closing bracket.

 

/ - Opening delimiter

^ - Anchor pattern at the start

(?:ftp:\/\/)? - A non-capture group to optionally match ftp://

([^/]+)@ - Capture one or more characters that isn't a forward slash, followed by an @

/i - The ending delimiter and the case-insensitive modifier

Link to comment
https://forums.phpfreaks.com/topic/203012-php-regex-help/#findComment-1063815
Share on other sites

(?:ftp://)?([^/]+)

 

Match the regular expression below «(?:ftp://)?»

  Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

  Match the characters “ftp://” literally «ftp://»

Match the regular expression below and capture its match into backreference number 1 «([^/]+)»

  Match any character that is NOT a “/” «[^/]+»

      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

Link to comment
https://forums.phpfreaks.com/topic/203012-php-regex-help/#findComment-1065025
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.