in reply to positive integer identification (regular expression question)

What this:
/^-?\d*(\.0*)?$/
matches is anything that is an integer (incorrectly matches a blank string though). Whereas this:
/^-?\d+(\.0*)?$/
matches an integer

but in answer to your query all you would have to do to check for just negative integers is make it so the - is not optional in the regular expression.

As always, perlretut and perlre are good reads

-enlil

Replies are listed 'Best First'.
Re: Re: positive integer identification (regular expression question)
by noggon (Initiate) on Feb 06, 2003 at 22:06 UTC
    Thank you all - it has been an enlightening and humble experience being in your company and reading your wisdom.

    Thank you.

    Scott

Re: Re: positive integer identification (regular expression question)
by CountZero (Bishop) on Feb 06, 2003 at 21:56 UTC

    Is there any reason why capturing parentheses are used around \.0*

    Also the ? does not make the preceeding pattern optional. As per the Camel book p. 39 "you can force nongreedy, minimal matching by placing a question mark after any quantifier". A ? not immediately preceeded by a quantifier thus has no sense.

    In order to make the preceeding pattern optional use the *-quantifier, which means zero or more times.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      If I do this (\.0*)*, then the pattern (\.0*) will match 0 or more times. Which would mean that .0 would match but so would .0.0.0 and .000.00 . These latter matches are not part of what is considered to be at the end of an integer. The ? on the other hand means whatever pattern is before me try to match it 1 or 0 times (in this instance making it optional). Unless ? is used afer a quantifier (e.g. +, or ?, or {min,max}). I could alternatively have used (\.0*){0,1} in this instance.

      on the other hand if I were to have ?? then it would try to match 0 or 1 time (in that order where as before (i.e, the single ? )it would try to match it 1 time and failing that it would ignore it was there, making it optional). I understand I might be unclear but I can further clarify if needed. I know this explained in the Mastering Regular Expressions book, but I have not quite yet located it in the Camel book (i will update when/if I find it)

      update: As for you question though there is no reason they are capturing parens it could just as well be (?:\.0*)? so they don't have to be capturing parens but I did not figured it mattered in this case, as I am lazy I dont normally put the ?: in the parens to make them non-capturing, unless I care what goes into the $1,$2,... series of variables, that are returned by the capturing parens.

      As for it location in in the Camel book (3rd edition) refer to page 142 at the paragraph starting with "Another thing you'll see are what we call quantifiers, ..."

      -enlil

        You're quite right about the use of ?. I forgot that it stands for {0,1}.

        What would you say about /^[-+]?\d+\.?0*$/? No capturing or clustering parentheses necessary and it will accept an (optional) plus-sign as well.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law