Reply to Re: regular expression help

Your name:

Reply:


Posted by Tim Hammerquist on 09/15/05 10:09

["Followup-To:" header set to comp.lang.perl.misc.]

www.douglassdavis.com <doug@douglassdavis.com> wrote:
> I am using the preg_match function (in PHP) that uses perl
> regular expressions. Apparently I don't really understand
> regular expressions though. Could some one explain this?

GAH! Interesting way to rationalize a cross-post, but let's see
if we can sort this out... :)

> If this is the regular expression
>
> /^\s*(\d+\.\d+)|(\.\d+)|(\d+)\s*$/
>
> How does this:
>
> 40:26:46.302N 79:56:55.903W
>
> match? I thought when I added the ^ and $ that meant it had
> to match the whole thing? It seems to only be matching .302

It's the placement of your alternators ('|') that's messing you
up. Because the |'s aren't inside any parens, the regex is
split up into the following three sub-expressions and tried
sequentially:

/^\s*(\d+\.\d+)/ # doesn't allow for the ':'
/(\.\d+)/ # matches because the anchors are excluded
/(\d+)\s*$/ # never gets a chance to match, but
# wouldn't anyway because it doesn't
# allow for the 'W'

You're probably looking for something more like:

/^\s*(\d+\.\d+|\.\d+|\d+)\s*$/

or

/^\s*(?:(\d+\.\d+)|(\.\d+)|(\d+))\s*$/

depending on what you want to do.

However, since you never actually stated what you *want* to
match, I have no way of knowing what regex you need. I can only
point out the probably cause of confusion. I.e., put your |'s
inside a set of parentheses. :)

HTH,
Tim Hammerquist

[Back to original 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

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