开发者

Regex for a Function Call?

I'm looking to simply pull some quoted text out of a function call and was wondering if I could get some help with regex?

The string would look something like this: 'MyFunction("MyStringArg");'

I would, essentially, like to scan a file for any lines that call 'MyFunction', and then capture the string literal within the quotes.

Follow-up Question

How would I go about avoiding commented lines with this?

Update

I was able to solve my problem with:

MyFunction\s*\(\s*"(.*?)\"\s*\)\s*;

Tha开发者_StackOverflow中文版nks @devyndraen and everyone for your help!


I'm not sure what kinds of requirements for formatting you have, so I included the assumption that there could be any amount of space in the normal programming places there could be some.

The resultant string will be in the \1 backreference.

MyFunction\s*\(\s*"(.*?)\"\s*\)\s*;

http://rubular.com/r/qVsaqJS6gJ


I would suggest this non-greedy regex with flag s (DOTALL in Java) (assuming there are no comments inside the parenthesis of this function call:

$regex = '/MyFunction.*?\(.*?"(.*?)".*?\).*?;/s';

If you use preg_match($regex, $str, $matches) then argument will be available in $matches[1].


To compensate for commented lines or blocks, you'd first need to filter the file to remove all comments before you apply the regex. For PHP, you could use the following:



$example='
line 1
line 2 // comment 1
line 3 # comment 2
// comment 3.1
# comment 3.2
/*
   comment 4.1
   comment 4.2
*/
line 9 /* comment 5.1
comment 5.2
*/';

echo '<h3>Example Text</h3><pre>'.$example.'</pre><hr>';

$regex='/
    (?x)
    (?:
        # single-line inline comments beginning at col#1
        (?s)
        (?:\\/\\/|\\#)
        [^\\n]+
        \\n
    |
        # single-line inline comments beginning after col#1 
        # preserve leading content
        (?m)
        ^
        (.+?)
        (?:\\/\\/|\\#)
        .*?
        $
    |
        # multi-line comments
        (?s)
        \\/
        \\*
            (?:.|\\n)*?
        \\*
        \\/
    )
/x';

echo '<h3>Regular Expression</h3><pre>'.$regex.'</pre><hr>';

$result=preg_replace( $regex, '$1', $example);

echo '<h3>Result</h3><pre>'.$result.'</pre><hr>';

which produces:


Example Text

line 1
line 2 // comment 1
line 3 # comment 2
// comment 3.1
# comment 3.2
/*
   comment 4.1
   comment 4.2
*/
line 9 /* comment 5.1
comment 5.2
*/

Regular Expression

/
    (?x)
    (?:
        # single-line inline comments beginning at col#1
        (?s)
        (?:\/\/|\#)
        [^\n]+
        \n
    |
        # single-line inline comments beginning after col#1 
        # preserve leading content
        (?m)
        ^
        (.+?)
        (?:\/\/|\#)
        .*?
        $
    |
        # multi-line comments
        (?s)
        \/
        \*
            (?:.|\n)*?
        \*
        \/
    )
/x

Result

line 1
line 2 
line 3 

line 9


[^(]*("([^"]*)")

and then group number 1 will be the string in quotes. You'll have to enquote it again yourself though.

(this is not highly scientific, as in it is likely to pick up some things that you don't want)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜