You are here: Re: [PHP] Need help with ereg regular expression « PHP « IT news, forums, messages
Re: [PHP] Need help with ereg regular expression

Posted by Richard Lynch on 10/04/24 11:07

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация