Re: Need help with ereg regular expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



KHS wrote:

> Here is an example of the input:
> #include FT_FREETYPE_H
> #include <stdio.h>
> #include "freetype/freetype.h"
>
> Here is a snip of my code:
> $line = fgets($fp);
> $line = trim($line);
> $regexp = '^#include(:? | "| <)([^< >"]+)[> "]';
> //$regexp = '^#include( <| "| )[^< >"]+[> "]'; //Same result as above
> //$regexp = '^#include [< "]([^>" ]+)[> "]'; //Similar to above result
> ereg($regexp, $line, $inclistings);
> var_dump($inclistings);
>
> Here is the output I am getting:
> #include FT_FREETYPE_H
>
> #include <stdio.h>
> array(3) {
>   [0]=>
>   string(18) "#include <stdio.h>"
>   [1]=>
>   string(2) " <"
>   [2]=>
>   string(7) "stdio.h"
> }
>
> #include "freetype/freetype.h"
> array(3) {
>   [0]=>
>   string(30) "#include "freetype/freetype.h""
>   [1]=>
>   string(2) " ""
>   [2]=>
>   string(19) "freetype/freetype.h"
> }
>
> Here is the output I want:
> #include FT_FREETYPE_H
> array(3) {
>   [0]=>
>   string(?) "#include FT_FREETYPE_H"
>   [1]=>
>   string(?) "FT_FREETYPE_H"
> }
>
> #include <stdio.h>
> array(3) {
>   [0]=>
>   string(18) "#include <stdio.h>"
>   [1]=>
>   string(7) "stdio.h"
> }
>
> #include "freetype/freetype.h"
> array(3) {
>   [0]=>
>   string(30) "#include "freetype/freetype.h""
>   [1]=>
>   string(19) "freetype/freetype.h"
> }
>
> So how do I keep ereg() from thinking the first set of parentheses
> is a substring to export. In addition, how do I craft the $regexp to
> recognize the first input $line? I came up with the above $regexp lines
> by using kregexpeditor in kde.

I think if you wrap an extra parentheses around the whole mess, you'll get
what you want...  But that might only be with preg and friends...

On the other hand, I think you could safely do:

$regex = "^#include(.*)$";

and get what you want by using trim on the captured expression.

Unless you're trying to make sure it's kosher C syntax, as well as snag
the included thing...

Even then, you might be better off doing the above, and *THEN* deciding if
the thing after #include is kosher or not.

Separting the capture of the text, from validating that it's kosher text
can simplify your life immensely sometimes, even if it's "more code" to
type.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux