in reply to What does this regex do?

Thanks for your help everyone, this line looked really confusing at first. I was beginning to wonder what ? meant but now I know it means it's optional, which makes sense.

So let me get this straight. The part before the (? matches anything that begins with a dot, a digit or more and a -? If so, what does everything inside (?: ) do? Only thing I can think of is before it hits that group it will ONLY match a dot, a - or a set of digits and the group itself allows you to use any combination of them (ie -1.094).

Am I anywhere close? One other thing, you said it allows two decimal points, how can I only get it to accept one?

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Re: What does this regex do?
by dpuu (Chaplain) on Apr 01, 2003 at 19:54 UTC
    As a general rule, a good way to build complex regexes is to be long-winded. Don't optimize. List each case in full:
    $interest !~ ( ^ # start of line -? # optionally negative (?: \d+ # numbers with no decimal point | \d+\.\d+ # numbers with decimal point in middle | \.\d+ # numbers with decimal point at start | \d+\. # numbers with decimal point at end ) $ # end of line /x;
    From this starting point, you can then attempt to optimize some the cases, if you want. But there's no need to: perl will optimize them for you. --Dave