help-bash
[Top][All Lists]
Advanced

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

Re: Testing numeric data


From: Dennis Williamson
Subject: Re: Testing numeric data
Date: Sun, 22 Aug 2021 19:13:17 -0500

On Sun, Aug 22, 2021, 7:03 PM 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]+$'
> 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]+)?$'
>
> declare -i k=0
> case $frmt in
> ("flp") [[ "$1" =~ "$flp" ]] && k=1 ;;
> ("pvflp") [[ "$1" =~ "$pvflp" ]] && k=1 ;;
> ("ngflp") [[ "$1" =~ "$ngflp" ]] && k=1 ;;
> ("flpe") [[ "$1" =~ "$flpe" ]] && k=1 ;;
> ("pvflpe") [[ "$1" =~ "$pvflpe" ]] && k=1 ;;
> ("ngflpe") [[ "$1" =~ "$ngflpe" ]] && k=1 ;;
> (*) [[ "$1" =~ "$flp" ]] && k=1 ;;
> esac
>
> echo "$k"


Quoting the right side of the match changes it to an equality relationship
rather than a regex match. Try

("flp") [[ "$1" =~ $flp ]] && k=1 ;;


reply via email to

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