doufang2023
2015-10-09 04:49
浏览 18
已采纳

Regexp:我如何匹配函数调用及其上面的注释

#hello world calling

hello_world();

============================
//hello world calling

hello_world('hello');
==========================
/*
    Hello World Calling
    multiline comment
*/

hello_world('hello','world');

How can I match all 3 sections with different comment style along with function being called.

Along with that I want to capture comments as well as the arguments of the function . example

array(
    array(
        [0] => 'hello world calling';
        [1] => 'hello world calling';
        [2] => 'hello world calling multiline comment';
    )
    array(
        [0] => '';
        [1] => 'hello';
        [2] => 'hello world';
    )
)

Tried some regex but didnt got what I wanted

图片转代码服务由CSDN问答提供 功能建议

  #hello world calling 
 
hello_world(); 
 
 ======  ====================== 
 // hello world calling 
 
 
 
 
 
 
 
 
 
 
世界'('hello'); 
 =========  ================= 
 / * 
 Hello World Calling 
多行注释
 * / 
 
 
 
 
lolo_world('hello','world'); 
    
 
 

如何匹配具有不同评论风格的所有3个部分以及被调用的函数。

同时我要捕获 注释以及函数的参数。 示例

  array(
 array(
 [0] =>'hello world calling'; 
 [1] =>'hello world calling'; \  n [2] =>'hello world调用多行注释'; 
)
数组(
 [0] =>''; 
 [1] =>'hello'; 
 [2]  =>'hello world'; 
)
)
   
 
 

尝试了一些正则表达式,但没有得到我想要的东西

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyingbei1458 2015-10-09 06:15
    已采纳

    You can try something like this:

    (?:(?:#|\/\/)(.*?)|\/\*((?:.|
    )*?)\*\/)*
    *?\b(.*?)\((.*?)\);
    
    点赞 打赏 评论
  • dongrouyuan5685 2015-10-09 05:38

    I tested your text on https://regex101.com/#pcre and these are the closest that I came up with.

    For commets:

    /\#(.*)|\/\/(.*)|\/\*([\w\W]*)\*\//g
    

    The result:

    MATCH 1
    1.  [1-20]  `hello world calling`
    MATCH 2
    2.  [69-88] `hello world calling`
    MATCH 3
    3.  [141-188]   `
        Hello World Calling
        multiline comment
    `
    

    For arguments:

    /\((.*)\)/g
    

    The result:

    MATCH 1
    1.  [34-34] ``
    MATCH 2
    1.  [102-109]   `'hello'`
    MATCH 3
    1.  [204-219]   `'hello','world'`
    
    点赞 打赏 评论

相关推荐 更多相似问题