2

I am using bash 4.1.10(4)-release and I trying to use a regex to match against two capital letters [A-Z]{2} and then anything. So for example BXCustomerAddress, CAmaterial would be acceptable, but WarehouseMessage would not. I have the following script for testing purposes:

#!/bin/bash

if [[ "Ce" =~ [A-Z]{2} ]]; then
    echo "match"
fi

My questions are:

  1. When I run this script, why does it return that I have a match?
  2. Does anyone know how I could achieve my desired results?
2
  • Is that the whole input string? Try this: [A-Z]{2}.* Commented Aug 15, 2014 at 17:26
  • Check .bash_profile or .bashrc if nocasematch or nocaseglob is enabled there. Commented Aug 15, 2014 at 17:28

4 Answers 4

5

Looks like you have shopt nocaseglob turned on. Turn it off using:

shopt -u nocaseglob

Now [[ "Ce" =~ [A-Z]{2} ]] should not match and will return false.

Sign up to request clarification or add additional context in comments.

9 Comments

Already about to answer :) I assumed nocaseglob is only for glob patterns.
It seems both nocaseglob and nocasematch affect this behavior.
I just checked both options: $ shopt nocasematch $ shopt nocaseglob both were turned off. Then I tried to change the script by inlining both options: #!/bin/bash shopt -u nocaseglob shopt -u nocasematch if [[ "Ce"=~[A-Z]{2} ]]; then echo "match" fi But I get the same results.
@carl_corder Did you include whitespace around the =~ operator? Your comment indicates you didn't, although it was included in the original question. Without the whitespace, the expression is treated as a single non-empty string that always evaluates to true.
Interesting I used regedit to change the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\kernel to 0, rebooted the machine. confirmed that the value is indeed zero. Ran the script again, same problem. I will keep you guys updated if I figure it out.
|
2

Check the value of the shell option nocasematch:

$ shopt nocasematch
nocasematch     off

Comments

1

shopt nocasematch is probably set to on. Turn it off with

shopt -u nocasematch

From the Bash Reference Manual:

nocasematch

If set, Bash matches patterns in a case-insensitive fashion when performing matching while executing case or [[ conditional commands.

1 Comment

I added both options to the .bashrc and it is still matching. I failed to mention that I am running the shell through cygwin (if it matters).
0

After trying out many different combinations, this is what gave me the expected behavior:

#!/bin/bash
# [A-Z][A-Z] will not work
# [:upper:][:upper:] will not work
# [[A-Z]][[A-Z]] will not work
# [[:upper:]][[:upper:]] does work

echo "test one"
if [[ "CA" =~ ^([[:upper:]][[:upper:]])+ ]]; then
    echo "match"
fi

echo "test two"
if [[ "Ce" =~ ^([[:upper:]][[:upper:]])+ ]]; then
    echo "match"
fi

I get the expected results of:

test one
match
test two

Thanks for everyone's help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.