RE: Optimal Regex?

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

 



> 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);
> $expression = '([(][-]?[0-9]*[.0-9]*?,[-]?[0-9]*[.0-9]*?[)])';
> preg_match_all($expression, "$string", $matches);
> 
> 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?

Hi David,

Someone else may have a more direct answer (ie a tweak of your regex that
accounts for the possibilities that you've mentioned) but looking at your
email I immediately had a concern over "etc, etc".

Regular expressions rely very much on you being able to predict the
different possible cases you will be dealing with.

Looking at the examples you provided, it looks like you're (currently?)
concerned with anomalous spaces in your string.

A simple way of dealing with these would be to do:

$string = str_replace(" ", "", $string);

...before you attempt your regex match. This will remove all spaces from the
string, making it fit a more predictable pattern (one that your current
regex matches).

If, however, there are other possible anomalous characters you need to take
into account, it would be much more helpful if you could supply examples of
them.

Much warmth,

Murray
---
"Lost in thought..."
http://www.planetthoughtful.org

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