3

I am struggling with a regex in php to extract function arguments from a string. I am parsing a javascript file in php and want to extract 2 function arguments from a line. Here is an example of how the lines may look:

"Backbone.Radio( 'comments ').trigger("added:comment " ,function(){});"

From this line I want to extract the word comments and added:comment without any whitespaces or quotes.

I have tried with:

$arrMatches = array();
$strRegEx = "/\\(\\s*['\"]\\s*([^)]+?)\\s*['\"]\\s*/";
$nMatches = preg_match_all($strRegEx, $strLine, $arrMatches);

But this will give me something like: ( 'comments ' and ("added:comment "

I would appreciate some help with how to solve this.

2
  • is it not possible to 'clean' them after extraction. So just remove ( and ' and " after you've gotten your results? Or will this method affect it, as the comments itself may contain brackets? Commented Sep 5, 2016 at 12:37
  • I could clean them after extraction but i would prefer to have the regex only extract the parts i need so i do not need that extra step. Commented Sep 6, 2016 at 6:32

2 Answers 2

2

You could use:

<?php

$regex = '~\(\h*([\'"])(.+?)\h*(?!\\\\)(?=\1)~';
$string = "Backbone.Radio( 'comments ').trigger(\"added:comment \" ,function(){});";

preg_match_all($regex, $string, $matches);
print_r($matches[2]);
/*
Array
(
    [0] => comments
    [1] => added:comment
)
*/
?>

The regex looks for single/double quotes, captures them and looks for the next single/double quote which is not escaped. See a demo on regex101.com.


Warning: While this might work in this particular example, it is usually the last way to use a regex - have you considered using a parser instead?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! This works great. But what is the benefits of using if i use a parser and what parser could i use for this example?
@sudden01: Well, it totally depends on your Input strings. If they are all of the mentioned format, this will very likely work. Problems will only occur if you face nested brackets, e.g. Backbone.Radio('comment_function("testparam")') which is totally JS valid.
Ok, then i think regex will do fine. The lines i am checking will always have this format. Thanks again for your help i will mark your answer as accepted.
1

Based on your example, something like this would work:

$re = "/(?<=\\()(?:[ '\"]*)([\\w\\d\\:]+)(?=[ ]*['\"]*)/"; 
$str = "\"Backbone.Radio( 'comments ').trigger(\"added:comment \" ,function(){});\""; 

preg_match_all($re, $str, $matches);

you can add other items to the matching group as required.

1 Comment

Thanks for the response, this also works. Since i am no regex expert i do not know which solution is the best but they both work for my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.