Charabon
Charabon

Reputation: 787

Regex Validation Kornshell

I have the following REGEX expression

^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$

In an attempt to cover all eventuality of mobile number in the UK. While parsing this validation through a REGEX tester online, which works great I am having difficulty getting it to work correctly in cornshell

fn_validate_msisdn() {
MSISDN=$1
REGEX_PTN="^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$"

if [ `echo $MSISDN | egrep -c $REGEX_PTN` -gt 0 ]
then
    return 1
fi

return 0;
}

Being called by:

if [ ! `fn_validate_msisdn ${MSISDN}` ]
    then
     ...
    fi

However It always seems to fail, either with illegal syntax or always returning greater than one.

some test data:

447999999999 : OK
07999999999 : OK
4407948777622 : FAIL
43743874874387439843 : FAIL

Any Suggestions would be great

Upvotes: 0

Views: 63

Answers (1)

anubhava
anubhava

Reputation: 784888

Your function can be just this:

fn_validate_msisdn() {
   MSISDN=$1
   REGEX_PTN="^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$"
   echo "$MSISDN" | egrep -q "$REGEX_PTN";
}

then:

fn_validate_msisdn 43743874874387439843
echo $?
1
fn_validate_msisdn 447999999999
echo $?
0

Remember return status of 0 means success and 1 means failure here.

Upvotes: 1

Related Questions