Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 匹配逗号分隔字符串中的特定单词_Sql_Regex_Oracle_Regexp Like - Fatal编程技术网

Sql 匹配逗号分隔字符串中的特定单词

Sql 匹配逗号分隔字符串中的特定单词,sql,regex,oracle,regexp-like,Sql,Regex,Oracle,Regexp Like,我有一个允许的值列表,例如:猫、老鼠、狗和鹦鹉。 现在,我想在一个以分号(;)分隔的单元格中添加此列表中的许多值 这意味着我可以补充: 猫;狗;老鼠 猫;鹦鹉 老鼠;猫 鹦鹉 但我不能补充 狮子;猫 老鼠;鹦鹉;狮子 猫;(这意味着我不能在末尾添加分号) 猫;老鼠 );(这意味着我不能只添加分号) 我尝试使用这个函数regexp_编写一个约束,但效果不好 not REGEXP_LIKE (animal, '[^(the cat| the mouse |the dog|the parrot|; )

我有一个允许的值列表,例如:猫、老鼠、狗和鹦鹉。 现在,我想在一个以分号(;)分隔的单元格中添加此列表中的许多值

这意味着我可以补充:

猫;狗;老鼠

猫;鹦鹉

老鼠;猫

鹦鹉

但我不能补充

狮子;猫

老鼠;鹦鹉;狮子

猫;(这意味着我不能在末尾添加分号)

猫;老鼠

);(这意味着我不能只添加分号) 我尝试使用这个函数regexp_编写一个约束,但效果不好

not REGEXP_LIKE (animal, '[^(the cat| the mouse |the dog|the parrot|; )]', 'i')

注意:animal是我应用约束的列

在字符串前面加上分隔符,然后像这样匹配:

REGEXP_LIKE(
  ';' || animal,
  '^(;\s*(the cat|the mouse|the dog|the parrot)\s*)*$',
  'i'
)
更新

您可以使用第二个表(带有外键引用)或嵌套表:

CREATE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/

CREATE TABLE animals (
  id          number,
  animal_list stringlist
) NESTED TABLE animal_list STORE AS animals__animal;

ALTER TABLE ANIMALS__ANIMAL ADD CONSTRAINT animals__animal__chk
  CHECK ( TRIM( BOTH FROM LOWER( column_value ) )
            IN ( 'the cat', 'the mouse', 'the dog', 'the parrot' ) );

INSERT INTO animals VALUES ( 1, StringList( 'the cat', ' the dog ' ) );
-- Succeeds
INSERT INTO animals VALUES ( 2, StringList( 'the cat', 'the horse' ) );
-- Fails with ORA-02290: check constraint (TEST.ANIMALS__ANIMAL__CHK) violated
然后你可以做:

SELECT id,
       ( SELECT LISTAGG( COLUMN_VALUE, ';' ) WITHIN GROUP ( ORDER BY ROWNUM )
         FROM   TABLE( a.animal_list ) ) AS animals
FROM   animals a;
哪些产出:

ID ANIMALS
-- -----------------
 1 the cat; the dog 

对于正则表达式部分,我相信这是最简单的验证正则表达式,它不需要操作数据(注意,“;”后面有一个空格)


谢谢你的回答,这很有帮助。但是我已经声明,我希望能够插入空值或无值。我试着修改你的答案,就像这个regexp|u like(“;”| | | categories),^(;\s*(猫、老鼠、狗、鹦鹉)| null | |)\s*)*$,“I”)。但我现在可以插入null@Oracle中的VannessaKemeni
NULL
和空字符串是一样的,因此您可以执行以下操作:
检查(animal为NULL或类似REGEXP_的(…)
真的不是:我添加了约束animal为NULL,但当我想在顶部插入不起作用的值“NULL”。所以我在regexp_中添加了这个值,比如(…)。在我的真实示例中,我有fast16个单词,oracle说正则表达式太长了。我怎么办?@MT0你的回答正是我想要的。但问题是我的实际值列表太长,我得到错误ORA-12733:正则表达式太长。现在我想知道是否有其他方法可以解决这个问题而不用正则表达式。不要这样做。不要在一列中存储多个由某个字符分隔的值。阅读数据库规范化。不要沿着这条路走。
^(the cat|the mouse|the dog|the parrot|; )+$