Re: regexps [was: aterm] - off-topic, wot

Andre Pang (andre@drizzt.mindflux.com.au)
Thu, 15 Apr 1999 01:02:03 +1000


On Wed, Apr 14, 1999 at 09:46:09AM -0400, Simon Drabble wrote:

> > Does anyone have a regular expression that would identify this:
> > [+|-]digit
> > Testing for a digit or a leading + or - is easy, but I can't 
> > come up with a regular expression that will identify a string
> > as a digit that may or may not have a leading plus or minus sign.
> > Thanks.
> 
> Try:
> 
> (([+-][0-9]+)|([0-9]+))

    If your regular expression engine doesn't support +'s and |'s (eg some
versions of grep), try:

    [+-]?[0-9][0-9]*

> If you were testing whether a string is _only_ a number (i.e. does not
> contain alpha or punct characters other than `.') then you could put a $
> at the end of the above regexp. - testing for fp's would be easier then

    You'd also need to put a ^ at the beginning as well as a $ at the end..

    Or for a slightly more versatile regexp which picks up floating point
numbers as well as standard boring integers:

    [+-]?[0-9]*[0-9.]?[0-9][0-9]*

    (Thanks to CaT for helping me figure that one out!).

    - #ozone

--
   WWW:   http://www.afterstep.org/
   FTP:   ftp://ftp.afterstep.org/
   MAIL:  http://www.caldera.com/linuxcenter/forums/afterstep.html