Oracle Regexp 在 SQL 中失败
Oracle Regexp fails in SQL
我正在尝试使用此正则表达式语句:
select 1 from dual where regexp_like('040', '^[\d\*]{3}$');
没有返回输出但有趣的是:
select 1 from dual where regexp_like('040', '^[[:digit:]\*]{3}$');
有效。
为什么?
我在使用 Oracle 11.2。
这是因为 Oracle 仅支持 POSIX 正则表达式标准,而不支持您在第一个示例中使用的 Perl 语法。
Oracle 文档:http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm#CHDJGBGG
POSIX 正则表达式标准:http://pubs.opengroup.org/onlinepubs/007908799/xbd/re.html
编辑:正如 Alex Poole 指出的那样,自 Oracle 10gR2 以来,Oracle 实际上支持 Perl 正则表达式语法。在我的本地 11gR2 安装上尝试您的示例表明您的语法错误,以下工作正常:
SELECT 1 FROM dual WHERE regexp_like('040', '^\d{3}$');
在您的第二个示例中,您使用的是 POSIX 字符 class。在您的第一个示例中,您将 POSIX 和 'Perl-influenced extensions'. The matching character-list 混合在 [ ... ]
:
中
Matches any single character in the list within the brackets. The following operators are allowed within the list, but other metacharacters included are treated as literals ...
因此在 []
中,反斜杠元字符被视为文字反斜杠,您正在寻找任何字符 \
、d
或 *
,并且 not 不是您所期望的单个数字 - \d
只是不像括号内的那样解释。而且您的示例字符串中没有任何这些文字字符,因此找不到匹配项。
即使在您的第二个版本中,\*
也只会匹配那些文字字符,因此不会添加任何内容;除非你想匹配 '12*'
或 '1'
这样的值,这似乎不太可能。
所以您可能只想要更简单的:
select 1 from dual where regexp_like('040', '^\d{3}$');
或等效项:
select 1 from dual where regexp_like('040', '^[[:digit:]]{3}$');
我正在尝试使用此正则表达式语句:
select 1 from dual where regexp_like('040', '^[\d\*]{3}$');
没有返回输出但有趣的是:
select 1 from dual where regexp_like('040', '^[[:digit:]\*]{3}$');
有效。
为什么?
我在使用 Oracle 11.2。
这是因为 Oracle 仅支持 POSIX 正则表达式标准,而不支持您在第一个示例中使用的 Perl 语法。
Oracle 文档:http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm#CHDJGBGG POSIX 正则表达式标准:http://pubs.opengroup.org/onlinepubs/007908799/xbd/re.html
编辑:正如 Alex Poole 指出的那样,自 Oracle 10gR2 以来,Oracle 实际上支持 Perl 正则表达式语法。在我的本地 11gR2 安装上尝试您的示例表明您的语法错误,以下工作正常:
SELECT 1 FROM dual WHERE regexp_like('040', '^\d{3}$');
在您的第二个示例中,您使用的是 POSIX 字符 class。在您的第一个示例中,您将 POSIX 和 'Perl-influenced extensions'. The matching character-list 混合在 [ ... ]
:
Matches any single character in the list within the brackets. The following operators are allowed within the list, but other metacharacters included are treated as literals ...
因此在 []
中,反斜杠元字符被视为文字反斜杠,您正在寻找任何字符 \
、d
或 *
,并且 not 不是您所期望的单个数字 - \d
只是不像括号内的那样解释。而且您的示例字符串中没有任何这些文字字符,因此找不到匹配项。
即使在您的第二个版本中,\*
也只会匹配那些文字字符,因此不会添加任何内容;除非你想匹配 '12*'
或 '1'
这样的值,这似乎不太可能。
所以您可能只想要更简单的:
select 1 from dual where regexp_like('040', '^\d{3}$');
或等效项:
select 1 from dual where regexp_like('040', '^[[:digit:]]{3}$');