regexbashequalitybackreference

Write a back reference in regular terms to determine the equality of two numbers


Please tell, how it is possible to write a back reference in a regular expression to determine the equality of two numbers.

For example the person entered 24 and 24 appeared on the screen "ok", on the contrary when entered 24 and 23 person received "not ok"?

I tried as follows:

#!/bin/bash

read a
read b
if <("$a" == "$b")>.*<\1>
then
   echo ok
else
   echo not ok
fi

Solution

  • You only need a direct variable comparison:

    #!/bin/bash
    
    read a
    read b
    if [ "$a" -eq "$b" ];
    then
       echo ok
    else
       echo not ok
    fi
    

    enter image description here

    Back-reference is for complex requirements like this:

    https://javascript.info/regexp-backreferences

    Also regex is not used to compare different values. It is used to compare the incoming value with a predefined, expected or desired pattern like this example in which the expected pattern is a mail and the incoming value could be any string

    if [[ "$1" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]
    then
        echo "Email address $email is valid."
    else
        echo "Email address $email is invalid."
    fi