Forum Moderators: coopster

Message Too Old, No Replies

Not sure if this regular expression is correct

         

FiRe

9:52 am on May 30, 2009 (gmt 0)

10+ Year Member



I am trying to extract a number followed by the word "metres" in a string. Some have a space between the number and word and some dont. Here is my expression:

preg_match("/([0-9]*)(\s?)metres/is", $desc, $m)

Not sure this is quite working though. It should match:

5 metres
5metres
500 metres
500metres

Any ideas?

Little_G

10:06 am on May 30, 2009 (gmt 0)

10+ Year Member



Hi,

This works for me,

/([0-9]+)\\s?metres/i
.
I had to double escape the s, not sure why but it works.

Andrew

mehh

11:17 am on May 30, 2009 (gmt 0)

10+ Year Member



I had to double escape the s, not sure why but it works.

I think its because of double quotes (") so the '\s' get translated to just 's' before the regex is evaluated. So the regex above can be used or the double quotes can be switched for single.

henry0

11:28 am on May 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you are always looking for a few "metres"
then you could be on the right track
but did you consider that the non plural
for example .75 or 1 "metre" does not have an "s"

g1smd

11:42 am on May 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Changing s to s? will fix that

jkovar

2:07 pm on May 30, 2009 (gmt 0)

10+ Year Member



preg_match('#(\d+)\s*metres?#si', $str, $m);