user2947180
user2947180

Reputation: 11

Regexp : How can I match function calling and the comments above it

#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

Upvotes: 0

Views: 92

Answers (2)

Moishe Lipsker
Moishe Lipsker

Reputation: 3032

You can try something like this:

(?:(?:#|\/\/)(.*?)|\/\*((?:.|\n)*?)\*\/)*\n*?\b(.*?)\((.*?)\);

Upvotes: 1

ryubro
ryubro

Reputation: 338

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'`

Upvotes: 0

Related Questions