help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Testing numeric data


From: Kerin Millar
Subject: Re: Testing numeric data
Date: Mon, 23 Aug 2021 01:31:32 +0100

On Mon, 23 Aug 2021 00:02:31 +0000
hancooper via <help-bash@gnu.org> wrote:

> I have the following code that determines if a number in numeric.
> 
> But when I use
> 
> frmt="flp" and `$1` set to `5.3`.
> 
> `k` is still `0`
> 
> # floating point numbers in fixed-point format
> local flp='^[-+]?[0-9]*[.,]?[0-9]+$'

As was just mentioned by Dennis Williamson, be sure not to quote the right-hand 
side.

$ flp='^[-+]?[0-9]*[.,]?[0-9]+$'; [[ 5.3 =~ $flp ]]; echo $?
0

> local pvflp='^+?[0-9]*[.,]?[0-9]+$'
> local ngflp='^-[0-9]*[.,]?[0-9]+$'
> 
> # floating point numbers in exponential format
> local flpe='^[-+]?[0-9]*[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?$'
> local pvflpe='^+?[0-9]*[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?$'
> local ngflpe='^-[0-9]*[.,]?[0-9]+(?:[eE][-+]?[0-9]+)?$'

As concerns the last three expressions, there is no supported syntax to 
indicate than an atom should be non-capturing in ERE. That's a feature that can 
be found in other dialects, such as PCRE.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]