For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > June 2005 > [PHP] Regex problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [PHP] Regex problem
Fluffy Convict

2005-06-09, 8:55 pm

In my school's website, I've found a regex that scans files for
functions which name's end with "_ding". On a match, it scans the
JavaDoc style comment of the function and filers out any @user comments.

Perhaps the following example will explain this better:

---------------------------- PHP: -----------------------------
preg_match_all(
";
/\*\* #Comment start
(?P<Comment>.*?) #Comment contents
\*/ #End of the comment
\s*
function\s+(?P<Function>[a-z0-9_]*_ding) #Function name
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------

Let's assume $file is scanned with this regex and $file contains the
following function:

/**
* @description This is for internal use
* @user This is to inform the user
*/
function foo_ding() {
return 'foo!';
}

Then the output of the rexex would be "This is to inform the user". Now
my problem. It's really quite simple I imagine for somebody with more
regex experience than I have. I now want to match functions that START
with ding_ and not end with it. So the regex should match

/**
* @user Inform the user
*/
function ding_foo() {
return 'foo you too!'
}

Who knows how to rebuild the regex? I've tried the code here under, but
It failt to return the whole function name. $matches['Function'] returns
"ding_f" instead of "ding_foo". Who can help me rewrite this regex
properly? Your help would be greatly appreciated!

---------------------------- PHP: -----------------------------
preg_match_all(
";
/\*\* #Comment start
(?P<Comment>.*?) #Comment contents
\*/ #End of the comment
\s*
function\s+(?P<Function>ding_*[a-z0-9_]) #Function name
;xim",
$file,
$matches,
PREG_SET_ORDER
);
---------------------------------------------------------------
Xenophaw

2005-06-11, 3:55 pm


"Fluffy Convict" <fluffyconvict@hotmail.com> wrote:
> In my school's website, I've found a regex that scans files for functions
> which name's end with "_ding". On a match, it scans the JavaDoc style
> comment of the function and filers out any @user comments.
>
> Perhaps the following example will explain this better:
>
> ---------------------------- PHP: -----------------------------
> preg_match_all(
> ";
> /\*\* #Comment start
> (?P<Comment>.*?) #Comment contents
> \*/ #End of the comment
> \s*
> function\s+(?P<Function>[a-z0-9_]*_ding) #Function name
> ;xim",
> $file,
> $matches,
> PREG_SET_ORDER
> );
> ---------------------------------------------------------------
>
> Let's assume $file is scanned with this regex and $file contains the
> following function:
>
> /**
> * @description This is for internal use
> * @user This is to inform the user
> */
> function foo_ding() {
> return 'foo!';
> }
>
> Then the output of the rexex would be "This is to inform the user". Now my
> problem. It's really quite simple I imagine for somebody with more regex
> experience than I have. I now want to match functions that START with
> ding_ and not end with it. So the regex should match
>
> /**
> * @user Inform the user
> */
> function ding_foo() {
> return 'foo you too!'
> }
>
> Who knows how to rebuild the regex? I've tried the code here under, but It
> failt to return the whole function name. $matches['Function'] returns
> "ding_f" instead of "ding_foo". Who can help me rewrite this regex
> properly? Your help would be greatly appreciated!
>
> ---------------------------- PHP: -----------------------------
> preg_match_all(
> ";
> /\*\* #Comment start
> (?P<Comment>.*?) #Comment contents
> \*/ #End of the comment
> \s*
> function\s+(?P<Function>ding_*[a-z0-9_]) #Function name


Here it's the mistake. I think you would need ding_[a-z0-9]*. The pattern
you wrote would matches 'ding' followed by 0 or more '_' and a character
between a to z and 0 to 9. If you don't mind to catch any function name like
ding_, i would use ding_[a-z0-9]+, which search for at least 1 character
after the '_'.

I hope this will help you.

> ;xim",
> $file,
> $matches,
> PREG_SET_ORDER
> );
> ---------------------------------------------------------------



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com