check for numeric content in a shell script (FreeBSD sh)
parv at pair.com
parv at pair.com
Fri Jun 25 18:50:52 UTC 2010
in message <20100624192256.GF557 at libertas.local.camdensoftware.com>,
wrote Chip Camden thusly...
>
> On Jun 24 08:39, Parv wrote:
> > in message <20100624183407.GA49923 at holstein.holy.cow>,
> > wrote parv at pair.com thusly...
> > >
> > > # Matches a number, either positive (without '+' sign) or
> > > # negative, which is either a whole number; or a real number
> > > # ending with decimal point, or a real number with or without
> > > # leading digits before the decimal point.
> > . ^
> > . ^ plural
> > > ^
> > > -?
> > > (
> > > [0-9] [.]? [0-9]*
> > > |
> > > [0-9]? [.] [0-9]+
> > . ^
> > . ^ oops
> >
> > Please change the immediately above regex portion to ...
> >
> > [0-9]* [.] [0-9]+
...
> We still need to be able to handle numbers without a decimal.
First alternative above handles that ...
[0-9] # Match 1 digit,
[.]? # followed by an optional decimal,
[0-9]* # followed by any number of optional digits.
> Try this:
>
> [0-9]*\.?[0-9]+
If it is really /^[0-9]*\.?[0-9]+$/, then it does not match
a negative number or a number ending with a decimal (e.g. 8.).
> The question mark says "0 or 1"
> >
> > > )
> > > $
Annotated regex now is ...
^ # Anchor at the beginning of string;
-? # followed by an optional -ve sign;
( # start grouping|alternatives;
[0-9] # match 1 digit,
[.]? # followed by an optional decimal,
[0-9]* # followed by any number of optional digits;
| # OR,
[0-9]* # match any number of optional digits,
[.] # followed by 1 decimal point,
[0-9]+ # followed by 1 or more digits;
) # end of grouping;
$ # anchor at the end of the string.
- parv
--
More information about the freebsd-questions
mailing list