The expression itself works for me:
dfs:~# gawk --posix '/^[A-Z]{2,4}$/ {print "Yes"}'
AAAA
Yes
AA
Yes
TT
Yes
tt
YY
Yes
yy
Your problems may be caused by two things. Either you accidentally set the IGNORECASE
awk variable or otherwise turned of case insensitive operation (BTW IGNORECASE
doesn't work with --posix
, but does with --re-interval
, which enables the braces in regular expressions too), or it is a classic problem of locale's collating sequence (because gawk does locale aware character comparison), which means the lowercase characters compare between some uppercase characters. Quote from the relevant part of the manual:
Many locales sort characters in
dictionary order, and in these
locales, ‘[a-dx-z]’ is typically not
equivalent to ‘[abcdxyz]’; instead it
might be equivalent to
‘[aBbCcDdxXyYz]’, for example. To
obtain the traditional interpretation
of bracket expressions, you can use
the C locale by setting the LC_ALL
environment variable to the value ‘C’.