Skip to Main Content
  • Questions
  • Regex for comma separated strong with predefined list of words that are allowed.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Vannessa.

Asked: April 05, 2017 - 1:18 pm UTC

Last updated: April 13, 2017 - 2:38 am UTC

Version: Oracle Database Express Edition

Viewed 1000+ times

You Asked

Hello,

With this regex : not REGEXP_LIKE (categories, '[^(subcat1| subcat2 |subcat3|null; )]', 'i'), i can specified what kind of values i can be used and this are separated with ';'.
but i can do that
insert into regexText Values(';') 
insert into regexTest Values('subcat1 subcat2)
insert into regexTest Values ('subcat1;)
insert into regexTest values('subcat1;null)


but insert into regexTest Values ('null') is allowed
and i want to correct my regex so that this insertion are not allowed.

Therefore i do that

Create Table regexTest(
categories VARCHAR2(1000) constraints check_ValidValue check(not regexp_like(categories,'^[(subcat1|subcat2|subcat3|null)];[(subcat1|subcat2|subcat3)]*;[(subcat1|subcat2|subcat3)]*')) 

)


but this did not work how i have expected.

----------------------------------------------------------------------------------------------------------------------------------
Hi chris ,
the purpose of this task is to add comma delimited string in a cell.
I have to determine a list of value that can be add in a cell.Although there are many string to be add.
e.g : the allowed value are : subcat1, subcat2,subcat3,subcat4.
i can insert in a cell : subcat1;subcat2;subcat3 Or subcat1 or subcat2;subcat1 or the value null. And when i insert the value null i can not insert anything else.

I can not insert: subcat1;subcat5 or subcat5 or subcat1; or ;

This symbols are to be allowed in the string : &,- and spaces.
And after ';' i can used spaces or not.

allowed values : subcat1
subcat2
subcat3
subcat4
The Data
A & B
A-B

Code that meets requirements :
insert into regexText Values('subcat1') 
insert into regexText Values('subcat1; subcat3') 
insert into regexText Values('subcat3; subcat4') 
insert into regexText Values('null') 
insert into regextext Values ('the data; subcat4)
insert into regexText Values('A & B; the data') 
insert into regexText Values('A & B; A-B; subcat2') 


Code that does not meet requirements:
insert into regexText Values(';') 
insert into regexText Values('subcat1;') 
insert into regexText Values('null; subcat4') 
insert into regexText Values('subcat4; null') 
insert into regexText Values('subcat7') 
insert into regexText Values('subcat1; subcat6) 


I thought that with a regex i can solve this problem but it appears difficult.
I don't know if there are an another way to solve this problem.
Thanks for the answer

with LiveSQL Test Case:

and Chris said...

Seriously. Rethink your data model. Storing comma, colon or any other character separated values is horrible, terrible idea. This will cause you pain later down the line. Using the string "null" instead of the value null is also questionable.

Anyway, if you know what the allowable values are, specify them using an IN list:

set define off
create table regexText (
  x varchar2(20)
);

alter table regexText add constraint c check (
  x in ('subcat1', 'subcat1; subcat3','subcat3; subcat4', 'null', 
        'the data; subcat4', 'A & B; the data', 'A & B; A-B; subcat2') 
);


Or, for a more flexible, data driven approach, create a lookup table of allowed values. Then place a foreign key from your table to this lookup:

create table allowed_vals (
  x varchar2(20) not null primary key
);

insert into allowed_vals Values('subcat1') ;
insert into allowed_vals Values('subcat1; subcat3') ;
insert into allowed_vals Values('subcat3; subcat4') ;
insert into allowed_vals Values('null') ;
insert into allowed_vals Values ('the data; subcat4');
insert into allowed_vals Values('A & B; the data') ;
insert into allowed_vals Values('A & B; A-B; subcat2') ;
commit;

alter table regexText add constraint fk 
  foreign key (x) references allowed_vals (x);

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Vannessa Kabiwo, April 12, 2017 - 7:04 am UTC

Hello chris,
thanks for the answer.
I know that the data model is not good but i have to do so for the project.
the answer is meaningful but i have normally 16 values. And when i follow your answer i can have almost 200 combinations. i can add one value, two value or three values and this answer is not good for this case.
Connor McDonald
April 13, 2017 - 2:38 am UTC

Option 1:

Why not generate the 200 values once, and then store them in a lookup table ? Because otherwise, every time something changes you'll need to rethink the regexp expression

Option 2:

Combine several regexp's to suit each subset of your criteria, and your constraint would be:

regexp_like(cat,'[regexp expression1]') or
regexp_like(cat,'[regexp expression1]') or
regexp_like(cat,'[regexp expression1]') or
etc

(But for me ... I'd be going option 1)