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.

Link to comment
https://forums.phpfreaks.com/topic/269214-understanding-regular-expression/
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.

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.

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.