dsaba Posted November 3, 2007 Share Posted November 3, 2007 I know [a-z]{3} will match: 'hey' I want to say [a-z]{any number of chars} the reason is i'm parsing through a php file and want to match all functions here's a sample string function funcName(list of args) { code body } any ideas with this? -thanks this doesnt quite work: /[fF]unction (.*) \((.*)\) \{(.*)\}/ Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 3, 2007 Share Posted November 3, 2007 I'm not really sure what you mean, but to match any number of characters in a given character range you use *: preg_match("/^([a-z]*)$/",$string) The above regex will match 'asdfaslekehe', 'a', '' ect. Any number of characters as you said. Quote Link to comment Share on other sites More sharing options...
dsaba Posted November 3, 2007 Author Share Posted November 3, 2007 well... I dont know how to put this without sound as if I want you to write it for me.. I want to match any number of the following patterns in a php file function funcName(list of arguments) { code body } look familiar, well its the pattern for writing functions in php funcName can be a-z lowercase or upper and any number of chars list of arguments is the same except it includes some other special chars like ',' and '=' code body is just about anything and any number of chars and it ends with a final } how to differentiate between last brackets (}) of the code body than the real last bracket of the actual function, i'm clueless but if you show me some regex to match that above describe pattern, I will most def. learn from it -thank you Quote Link to comment Share on other sites More sharing options...
dsaba Posted November 3, 2007 Author Share Posted November 3, 2007 I tried this: <?php $string = ' $bla = \'this other code\'; function funcName(list of arguments) { if (lala) { maybe; } else { ok; } return $set; }'; $find = preg_match_all('/function ([a-zA-Z0-9_]*) \{(.*)\}[^}]*/', $string, $matches); print_r($matches);?> it returned this: Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) [3] => Array ( ) ) what do I need to change? Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 3, 2007 Share Posted November 3, 2007 This should do what you want: <?php $string = 'function tisoglol($lol="dild", $laks="fersk")'; $regex = "/^function ([a-z]+)\(([a-z=, \"\$]*)\)$/i"; preg_match_all($regex,$string,$matches); print_r($matches); ?> It will match bad function syntax though since the argument part is only specified as a string of 0 or more occurrences of $, a-z, ", ,(comman). So this will match too: "function somefunction("lol=fer$sk)" Quote Link to comment Share on other sites More sharing options...
dsaba Posted November 4, 2007 Author Share Posted November 4, 2007 thanks for the interest but you've misunderstood me (I need to work on this) your regex grabs the function name and the arguments of the function I want to grab the function name, the arguments, and the code body of the function in other words the whole pattern, like I mentioned in the above posts verbatim what I mean is this: $bla = \'this other code\'; function funcName(list of arguments) { if (lala) { maybe; } else { ok; } return $set; }'; I would want to put function funcName(list of arguments) { code body } into an array, where in each array it grabs another pattern like this, this particular example (since there is only 1 function in the string) should produce an array with 1 element in it Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 4, 2007 Share Posted November 4, 2007 I don't have the time to work all of that out, but can see already that this would be extremely difficult to grab all in one REGEX pass. You're most likely going to have to do multiple passes at each function and slowly build the final array. PhREEEk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.