'1A' is not accepted in [:alnum:] POSIX Character Class under Oracle regular expresssion

177 Views Asked by At

Why is '1A' not considered in alphanumeric class?

SELECT 'yes'
FROM dual
WHERE REGEXP_LIKE('1A', '[:alnum:]');
1

There are 1 best solutions below

1
On BEST ANSWER

A pair of unescaped [...] creates a bracket expression. POSIX character classes, like [:alpha:], [:alnum:], can only be declared inside a bracket expression.

[:digit:] is a POSIX character class, used inside a bracket expression like [x-z[:digit:]].

Use the POSIX character class inside a bracket expression:

SELECT 'yes' FROM dual WHERE REGEXP_LIKE('1A', '[[:alnum:]]')

enter image description here

See an online demo.

('1a', '[:alnum:]') seems to acceptable.

Note that when you use '[:alnum:]', the regex engine parses it as a regular bracket expression matching any characters/character ranges defined inside the bracket expression. I.e. the [:alnum:] regex matches any single char that is either :, a, l, n, u or m. Since your input has a (1a) the regex engine returns a valid match once it encounters a in the input.