You are here: Re: [PHP] Regex problem « PHP Language « IT news, forums, messages
Re: [PHP] Regex problem

Posted by Xenophaw on 11/09/44 11:18

"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
> );
> ---------------------------------------------------------------

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация