Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Question about REGEX Patterns and Case Sensitivity?

$
0
0
Hello All,

I'm in the middle of a script and I'm doing some checks with REGEX (i.e. using the '[[' ).

I'm wondering if this example is correct or if its just a coincidence. But I thought that if I did not use the "shopt -s nocasematch"
that at least the first one should print "FALSE" but it prints "TRUE"..?

For Example:
Code:

#!/bin/bash

MY_VAR="HELLO"

### This prints "TRUE"
PATTERN_1="^[a-z]*"
if [[ $MY_VAR =~ $PATTERN_1 ]]
 then
    echo "TRUE"
else
    echo "FALSE"
fi

echo "-------------------------"

### This prints "FALSE"
PATTERN_2="^[A-z]*"
if [[ $MY_VAR =~ $PATTERN_2 ]]
 then
    echo "TRUE"
else
    echo "FALSE"
fi

echo "-------------------------"

### This prints "TRUE"
PATTERN_3="[a-Z]*"
if [[ $MY_VAR =~ $PATTERN_3 ]]
 then
    echo "TRUE"
else
    echo "FALSE"
fi



The OUTPUT:
Code:

TRUE
-------------------------
FALSE
-------------------------
TRUE

I remember being told before that the pattern "[A-z]" is NOT the same as doing "[A-Za-z]" like it would be in Perl...
So I'm wondering why the pattern "[a-Z]", which is the last if statement in the code above, returns "TRUE", when
the 2nd if statement above "[A-z]" returns "FALSE"...?

I tried changing the Variable "$MY_VAR" from all upper case to all lowercase, but I still get the same output...
And lastly, if I include the "shopt -s nocasematch" they all return "TRUE"...


If anyone has any thoughts/suggestions that would be great!

FYI:
Bash Version:
4.1.10


Thanks in Advance,
Matt

Viewing all articles
Browse latest Browse all 16232

Latest Images

Trending Articles



Latest Images