我想在 oracle 数据库中使用 REGEXP_INSTR()
来检查小写/大写字符。我知道 [:upper:]
和 [:lower:]
POSIX 字符类,但我使用了 a-z
这给了我真正奇怪的结果我不明白。谁能解释一下?
SELECT REGEXP_INSTR('abc','[A-Z]',1,1,0,'c') FROM DUAL
-- Got 2, expected 0
SELECT REGEXP_INSTR('zyx','[A-Z]',1,1,0,'c') FROM DUAL
-- Got 1, expected 0
SELECT REGEXP_INSTR('ABC','[a-z]',1,1,0,'c') FROM DUAL
-- Got 1, expected 0
SELECT REGEXP_INSTR('ZYX','[a-z]',1,1,0,'c') FROM DUAL
-- Got 2, expected 0
SELECT REGEXP_INSTR('a3','[A-F0-9]',1,1,0,'c') FROM DUAL
-- Got 2, expected 2
SELECT REGEXP_INSTR('b3','[A-F0-9]',1,1,0,'c') FROM DUAL
-- Got 1, expected 2
SELECT REGEXP_INSTR('b3','[A-F0-9]') FROM DUAL
-- Got 1, expected 1 or 2
SELECT REGEXP_INSTR('a3','[A-F0-9]') FROM DUAL
-- Got 2, expected same as above
最佳答案
行为的原因是归类规则。查看NLS_SORT
documentation :
- If the value is BINARY, then the collating sequence for ORDER BY queries is based on the numeric value of characters (a binary sort that requires less system overhead).
- If the value is a named linguistic sort, sorting is based on the order of the defined linguistic sort. Most (but not all) languages supported by the NLS_LANGUAGE parameter also support a linguistic sort with the same name.
将 NLS_SORT
设置为 BINARY
以便 [A-Z]
可以按照与 ASCII 表中相同的顺序进行解析,
alter session set nls_sort = 'BINARY'
然后,您将获得一致的结果。
参见 online demo .
关于sql - Oracle REGEXP_INSTR() 和 "a-z"字符范围与预期不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57986830/