|
Posted by Fluffy Convict on 11/03/65 11:18
Xenophaw wrote:
> "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
>>);
>>---------------------------------------------------------------
>
>
>
Thanx, Xenophaw! That really dit the trick. I'm not quite there with my
regex knowledge...but practice makes perfect :)
Navigation:
[Reply to this message]
|