Re: Regular Expression Problem

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

 



On Thu, 2009-02-26 at 10:09 -0500, Alice Wei wrote:
> Hi, 
> 
>   I have two lines here as follows:
> 
> 1      -0.123701962557954E+03       0.460967618024691E+02
> -0.123547659000000E+03       0.462591090000000E+02
> 
> What I am trying to do here is to only have 
> 
> 1      -0.123701962557954E+03       0.460967618024691E+02 be the output so I can do further processing with it, however, with the code I have in the following, I have both lines in the output. Here is the code:
> 
>     $file = "test.txt";
>     $fp = fopen($file, "r");
>     $lines = file($file);
> 
> foreach($lines as $line_num => $line)
> {
> if (preg_match("/([^0-9]+\s+)(\-?\d+\.?\d+(\+?E?\d+)?){2}/", $line))
> 
> echo $line;
> 
> }
>   
> Could anyone please tell me that even when I specify that the line has to start with [^0-9]+\s with one occurrence, why I still get -0.123547659000000E+03       0.462591090000000E+02?
> 
> Thanks a lot for your help.
> 
> Alice
> 
> 
> _________________________________________________________________
> Express yourself with gadgets on Windows Live Spaces
> http://discoverspaces.live.com?source=hmtag1&loc=us
what about a regex something like:

/^(\d{1,})\s+([\d\.E\+\-])+\s+([\d\.E\+\-])+$/

The brackets () allow you to then extract the specific portions of the
string as you wish, so the first match is the whole line, the second is
the 1 (or whatever other number), the third is the first floating-point
number, etc.

I'm assuming that the 1 here is some sort of line number possibly? The
bit inside the curly braces ({1,}) just says to match the digit 1 or
more times. You can limit it by adding a number after the comma, so
{1,3} for 1-3 digits.

Hope this helps?


Ash
www.ashleysheridan.co.uk


-- 
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