Re: Optimal Regex?

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

 



Ok, so this is the actual code that I'm using.

<?

$string = "( 334, -53.44) ( 111 , 222 )";
$testStr = $string;
// $string = str_replace(" ", "", $string);
preg_match_all("-?([\d]+)?(\.[\d]+)?",$string, $matches);

var_dump($matches);


?>
and the output is this...

*Warning*: No ending delimiter '-' found in
*/home/da3/public_html/preg.php*on line
*6*
array(0) { }
 What am I doing wrong?
 On 9/26/05, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote:
>
> keep it on the list please.
>
> and you still didn't answer the question :-)
>
> <snip>
> > preg_match_all($expression, "$string", $matches);
> > ^-- why ? ...
> >
> > are you wrapping the variable in double quotes?
> </snip>
>
> ...and also from your code:
> $string = (334,-53.44),(111,222);
> ^-- er what is this 'string' about?
>
> David Pollack wrote:
> > I added the $string and $expression variables at the last minue for
> > readabilty, before I just had it all in the preg_match_all. I'm using a
>
> ah but they were not valid code - which can confuse matters.
> try to post code that runs if you can (i.e. test if you're providing
> a testcase) .. i guess you have to kind of flip that argument inside
> out when it comes to enquiring whether something might be a bug -
> there is still testing involved tho.
>
> <garanteed-top-quality-sarcasm-free>
> have you ever used 'php -r' - it's brilliant - especially when you
> fund out how easy it is to cut and paste into a bash[-like] shell
> (probably using Putty if/when your on windows). be ware of
> single quotes.
> </garanteed-top-quality-sarcasm-free>
>
> > simple test file for this. I used your example and it returned two
> > identical arrays with the correct information. Interesting.
>
> maybe my regexp is not only bad but also wrong [for
> grepping coordinates in a 'any' variation of '( 123 ,-45.78)' ] ?
>
> $coordRE = "(?:[ ]?[-+]?\\d+(?:\\.\\d+)?[ ]?)";
> $coordsRE = "#(\\({$coordRE},{$coordRE}\\))#";
>
> var_dump($coordRE, $coordsRE);
>
> ==========================================
>
> moulin@moulin~ -- Tue Sep 27 00:24:20
> $> php -r '
> $> $coordRE = "(?:[ ]?[-+]?\\d+(?:\\.\\d+)?[ ]?)";
> $> $coordsRE = "#(\\({$coordRE},{$coordRE}\\))#";
> $>
> $> var_dump($coordRE, $coordsRE);
> $>
> $> '
> string(30) "(?:[ ]?[-+]?\d+(?:\.\d+)?[ ]?)"
> string(69) "#(\((?:[ ]?[-+]?\d+(?:\.\d+)?[ ]?),(?:[ ]?[-+]?\d+(?:\.\d+)?[
> ]?)\))#"
>
>
>
> >
> > On 9/26/05, *Jochem Maas* <jochem@xxxxxxxxxxxxx
> > <mailto:jochem@xxxxxxxxxxxxx> > wrote:
> >
> > David Pollack wrote:
> > > I need to catch all instances of a coordinate in a give string.
> > So far I
> > > have this, which only works if the coordinate is in a certain format.
> > >
> > >
> > > $string = (334,-53.44),(111,222);
>
> ^-- er what is this 'string' about?
>
> > > $expression = '([(][-]?[0-9]*[.0-9]*?,[-]?[0-9]*[.0-9]*?[)])';
> >
> > here's a horrid little bit of .... for you :)
>
> <you could extract this code and run if you were really really bored>
>
>
> >
> > <?php
> >
> > $testStr = <<< EOD
> > > \$string = (334,-53.44),(111,222);
> > > preg_match_all(\$expression, "\$string", \$matches);
> > >
> > > This returns the correct strings (334,- 53.44) and (111,222), but
> > Id like
> > > all formats to work. Like (111, 222), ( 111 , 222 ), (111, 222 ),
> > etc, etc.
> > > How would I change my regex to accomplish this?
> > EOD;
> >
> > $coordRE = "(?:[ ]?[-+]?\\d+(?:\\.\\d+)?[ ]?)";
> > $coordsRE = "#(\\({$coordRE},{$coordRE}\\))#";
> > $matches = array();
> > preg_match_all($coordsRE, $testStr, $matches);
> > var_dump($testStr, $matches);
> >
> >
> > ?>
>
> </you could extract this code and run if you were really really bored>
>
>
> >
> > > preg_match_all($expression, "$string", $matches);
> > ^-- why ? ...
> >
> > are you wrapping the variable in double quotes?
> >
> > >
> > > This returns the correct strings (334,-53.44) and (111,222), but
> > I'd like
> > > all formats to work. Like (111, 222), ( 111 , 222 ), (111, 222 ),
> > etc, etc.
> > > How would I change my regex to accomplish this?
> > >
> > > --
> > > David Pollack
> > > DHPollack@xxxxxxxxx <mailto:DHPollack@xxxxxxxxx>
> > >
> > > www.atlspecials.com <http://www.atlspecials.com> <
> http://www.atlspecials.com>
> > <http://www.atlspecials.com>
> > > www.da3.net <http://www.da3.net> <http://www.da3.net> <
> http://www.da3.net>
> > >
> >
> >
> >
> >
> > --
> > David Pollack
> > DHPollack@xxxxxxxxx <mailto:DHPollack@xxxxxxxxx>
> >
> > www.atlspecials.com <http://www.atlspecials.com> <
> http://www.atlspecials.com>
> > www.da3.net <http://www.da3.net> <http://www.da3.net>
>
>


--
David Pollack
DHPollack@xxxxxxxxx

www.atlspecials.com <http://www.atlspecials.com>
www.da3.net <http://www.da3.net>

[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