I need to know whether a string contains only ASCII characters. So far I use this REGEX:
DECLARE
str VARCHAR2(100) := 'xyz';
BEGIN
IF REGEXP_LIKE(str, '^[ -~]+$') THEN
DBMS_OUTPUT.PUT_LINE('Pure ASCII');
END IF;
END;
/
Pure ASCII
' '
and ~
are the first, resp. last character in ASCII.
Problem is, this REGEXP_LIKE fails on certain NLS-Settings:
ALTER SESSION SET NLS_SORT = 'GERMAN';
DECLARE
str VARCHAR2(100) := 'xyz';
BEGIN
IF REGEXP_LIKE(str, '^[ -~]+$') THEN
DBMS_OUTPUT.PUT_LINE('Pure ASCII');
END IF;
END;
/
ORA-12728: invalid range in regular expression
ORA-06512: at line 4
Do anybody knows a solution which works independently from current user NLS-Settings? Is this behavior on purpose or should it be considered as a bug?
IF (ASCIISTR(str) = str) THEN
? – Demotic[ -~]
is printable ascii, but ascii is really 7-bit[\x00-\x7F]
– Caracole[\x20-\x7E]
Using byte syntax should never throw a range error. – Caracoleasciistr('\')
gives\005C
, presumably to avoid confusion as that has a special meaning in the output. – Deathbed[\x20-\x7E]
also raises ORA-12728 – Esemplastic\xDD
is not a character, it is a number, the code of the character. There is no translation from code to character, there is only translation from char to code, in regex landia. Where the range integer arithmetic is done. Hope you understand that. That's why the syntax\xDD
is unique to regex parsers, so language does not play a factor. – Caracole