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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.