Jump to content

Understanding Regular Expression


priti

Recommended Posts

Hi All,

 

I have a piece of code from php.net

 


if (!ctype_alnum($username) || !preg_match('/^(?:[a-z0-9_-]|\.(?!\.))+$/iD', $userfile)) {
   die("Bad username/filename");
}

 

I do not understand "/D" ! I know of /g, /i OR /m for multi line..

 

can someone please help me understanding this expression.

Edited by priti
Link to comment
Share on other sites

Flags aside,

"

^
 beginning of the string (sometimes line)
(?:...)
 group but don't capture
[a-z0-9_-]
 letters (including uppercase since you have /i), numbers, underscores, and hyphens
|
 or ('x|y' means 'x or y')
\.
 literal period
(?!...)
 negative lookahead ('the following must not be ...')
+
 one or more of the previous, which in this case applies to the first group

Summed up, the expression matches any sequence of letters/numbers/underscores/hyphens/periods but won't allow two (or more) consecutive periods.

Edited by requinix
Link to comment
Share on other sites

Also, using both ctype_alnum () and preg_match () is not necessary at all. The Regular Expression alone is enough.

In fact, the Regular Expression will never have any meaning in the code you posted, as the first command is way more restrictive. Since a period is not a part of the "alpha numerical" definition, the RegExp will never have the chance to test for periods.

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.