...

View Full Version : Regex Help



NancyJ
05-08-2008, 01:23 PM
I'm trying to create a regex to capture the comment blocks in a php file. Specifically blocks like:



/**
* some text here
* and some more
* etc
*/
function here()
{
}

/**
* some text here
* and some more
* etc
*/


Basically a block that starts with /** and ends with */ and can have any text in the middle

I tried this: /\*\*[.\r\n]+\*/ but that didn't work.

NancyJ
05-08-2008, 03:49 PM
I got it working but its a bit of a cludge - I used [^~] (on the grounds I wont be using ~ in my comments) instead of [.\r\n], for some reason . wasn't working in a character class.

This is what I'm currently using but I'd still like to fix it to be less cludgy.

/\*\*([^~]*?)\*/

Fou-Lu
05-08-2008, 04:05 PM
Ah, too much work. Trust me, I tried before php 5 and I hated it I recall.
I'm pretty sure you said your on php 5+ now right? Look into the 'Reflection' class in php, one of them is the ReflectionFunction, which will drag all the info out for you (where comment is one of its own so long as its /** .. */ style - only two stars at the start).
Try that nancy, you'll love all the other stuff it does too!


Here's a link directly to ReflectionFunction (http://ca3.php.net/manual/en/language.oop5.reflection.php#language.oop5.reflection.reflectionfunction)

shyam
05-08-2008, 04:05 PM
try /\/\*\*(.|[\r\n])+?\*\//g

Mwnciau
05-08-2008, 04:56 PM
If you want it less cludgy you could just use /\*\*.+?\*/ with the s modifier.

oesxyl
05-08-2008, 11:19 PM
As far as I know can't be done with a single regex. A solution, if this help you is that:


<?php
$texts = "/**
* some text here
* and some more
* etc
*/
function here()
{
}

/**
* some text here
* and some more
* etc
*/

/* Another style
*
*/
";

function markblock($s){
$r = preg_split("/\*\//",$s);
return array("cmt" => $r);
}

$comments = preg_split("/\/\*\*?\s*/",$texts);
$comments = array_map("markblock",$comments);
var_dump($comments);
?>

you can format the comments or sections for example to replace * and spaces/newlines with a single space....

to find and list comment sections


foreach($comments as $blk){
echo $blk['cmt'][0]."\n--------------\n";
}


same thing with sections what follow comments


foreach($comments as $blk){
echo $blk['cmt'][1]."\n--------------\n";
}
?>


regards

Fou-Lu
05-08-2008, 11:59 PM
I'm pretty sure when I used this method I did it in a single regexp. Let me think...


/**
* Function: MyFunc
* All that fun stuff here
*/
...



...
$matches = array();
preg_match('/\/\*\*(.*)\*\//ms', $inputStream, $matches);
$i = 0;
foreach ($matches AS $vals)
{
// Yes I see what you're getting at now, this may not work. Thats ok.
if ($i++ == 0)
{
continue;
}
// Do whatever you want but you'll need...
$vals = trim($vals, ' *');
}



I see what you mean now oesxyl, this may not work. I'm thinking that the resultant will always have too much data in it.


I just can't remember off hand if I used an ungreedy quantifier on it or not, I'm thinking that I did or I should only get one set of comments which would include tons of code in it as well.
This of course was only for doc style comments (/** ... */), with only the two leading stars (just like the reflection, it would fail if there were more)
Reflection usage (if you have it, its so much easier):


/**
* some text here
* and some more
* etc
*/
function here()
{
}

/**
* some text here
* and some more
* etc
*/

Now, you can either use the reflection on an individual function, or collect all that are defined:


// Lets get all the user defined ones by forcing the script to give it to us:
$myComments = array();
$defined = get_defined_functions(); // Built in since php 4.0.4
if (is_array($defined['user']))
{
foreach ($defined['user'] AS $userFunc)
{
$reflect = new ReflectionFunction($userFunc);
$myComments[$userFunc] = var_export($reflect->getDocComment(), 1);
}
}


That should give you an array of your comments with an associative function name in mycomments. There is a whole pile of stuff you can get with Reflection, and its awesome for TDD if you're into that



<?php
class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector
{
final private __clone()
public void __construct(string name)
public string __toString()
public static string export(string name, bool return)
public string getName()
public bool isInternal()
public bool isDisabled()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public mixed invoke([mixed args [, ...]])
public mixed invokeArgs(array args)
public bool returnsReference()
public ReflectionParameter[] getParameters()
public int getNumberOfParameters()
public int getNumberOfRequiredParameters()
}
?>

Been awhile since I've reflected too, so you may need to tweek it a bit.

oesxyl
05-09-2008, 12:21 AM
That should give you an array of your comments with an associative function name in mycomments. There is a whole pile of stuff you can get with Reflection, and its awesome for TDD if you're into that

probably are more solution to this problems. I don't look for some parser extensions for php, for example, :).
But all this depends of what NancyJ want to achive. If want to build some CASE for php, probably the best solution is reflection.
If only want to extract the comments block, a documenting system, one solution look like mine or better a already build package like doxygen( I don't know if there is a version for windows but I guess it is).

regards

Fou-Lu
05-09-2008, 12:31 AM
probably are more solution to this problems. I don't look for some parser extensions for php, for example, :).
But all this depends of what NancyJ want to achive. If want to build some CASE for php, probably the best solution is reflection.
If only want to extract the comments block, a documenting system, one solution look like mine or better a already build package like doxygen( I don't know if there is a version for windows but I guess it is).

regards

Agreed. PHP is so expansive I don't think I'll ever get to half of the built in stuff, even with 8 years of usage >.<. I suggest reflection simply because I do so much object work now - so easy to build my documentation up, otherwise I'd be too lazy to document my methods, lol.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum