Regex to call a function?

I just want to get the quoted text from the function call and was wondering if I can get some help in the regular expression?

The line will look something like this: 'My_function ("MyStringArg");'

I would, in essence, like to scan the file for any lines that call "MyFunction", and then fix the string literal inside the quotes.

Follow-up question
How can I avoid comments with this?

Update
I was able to solve my problem:
MyFunction\s*\(\s*"(.*?)\"\s*\)\s*;

Thanks to @devddraen and everyone for your help!

+3
source share
4 answers

, , , - .

\1 backreference.

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

http://rubular.com/r/qVsaqJS6gJ

+2

s (DOTALL Java) ( :

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

preg_match($regex, $str, $matches), $matches[1].

+1

, , , . PHP :



$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>';

:


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
*/

/
    (?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

line 1
line 2 
line 3 

line 9
0
[^(]*("([^"]*)")

1 ​​. .

( , , , , )

-1

All Articles