duanlang0025 2012-11-17 08:16
浏览 53

正则表达式如何使正则表达式以秒/ **为起点

Here is the $source example

/**
 * These functions can be replaced via plugins. If plugins do not redefine these
 * functions, then these will be used instead.
 */

if ( !function_exists('wp_set_current_user') ) :
/**
 * Changes the current user by ID or name.
 *
 */
function wp_set_current_user($id, $name = '') {

Attention: some don't have the function_exists line.

For my special purpose, I'm trying to parse the docblock with regular expression.

Here is the regex

$t = preg_match_all("@(/\*\*.*?\*/
function\s.*?\(.*?\))\s{@mis",$source,$m);

I expect to get:

    /**
     * Changes the current user by ID or name.
     *
     */
    function wp_set_current_user($id, $name = '') {

but instead, it returns me the whole code example.

Any help would be appreciated.


I find out some people ask me my purpose, I don't think this is important here though.

I'm using geany and I find out existing wordpress code hint isn't complete.

And the docblock parsers I found that don't parse function name and function arguments.

So I try to parse them on my own.

the code hint format of geany is

wp_set_current_user|Changes the current user by ID or name.|($id, $name = '')|

However, my point of this question is how to make regex take second "/**" as starting point? I'm sorry for my poor English that confused you all.

  • 写回答

2条回答 默认 最新

  • douqin6785 2012-11-17 08:44
    关注

    You can parse comment out by regexp like this (check out Regex look around tutorial):

    /\*\*/(?:(?:.(?!\*\*/))*)\*\*/
    

    Then any number of white spaces can occur:

    [\s]*
    

    What keywords can function have in php? static, virtual, final, public, private, protected correct me if I'm forgetting something.

    (?:(?:static|virtual|final|public|private|protected)\s+)*
    

    Okay, now function header and braces:

    function\s+(?P<name>\w\d_+)\s*\(...\)
    

    The ... parts get's complicated because it can contain default value which can be complicated php string ($remove_characters = '\'" '), so parsing value (string, string, number, constant):

    "[^"\\\\]*(?:\\\\.[^"\\\\]*)*"
    \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'
    [\d.]+
    \w+
    

    Resulting to one large value regexp:

    ("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'|[\d.]+|\w+)
    

    And every function argument has a format $var or $var = data (of course any number of spaces + I'm omitting array $input = array()) so this is simplified var name matching:

    \\$[\w_][\w\d_]*
    

    Type matching:

    ([\w_]+\s+)?
    

    So function arguments can be:

    \s*([\w_]+\s+)?(\\$[\w_][\w\d_]*|\\$[\w_][\w\d_]*\s*=\s*<value>)
    

    And complete regexp for function would look like:

    function\s+(?P<name>\w\d_+)\s*\(\s*|<argument>((,<argument>)*)\)
    

    I won't be testing those regexp for you, it's your job to do so at this point, my goal was to show you what you need if you want to do this really correctly (but feel free to edit my answer if you find a mistake).You may also use really simplified version (like just one regexp for function arguments eating everything).

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法