WITH t AS ( SELECT 'ABRBRBDS' v FROM dual UNION ALL SELECT 'A' FROM dual UNION ALL SELECT 'AR' FROM dual UNION ALL SELECT 'AB' FROM dual UNION ALL SELECT 'BR' FROM dual UNION ALL SELECT 'R' FROM dual UNION ALL SELECT 'BRB' FROM dual UNION ALL SELECT 'BDS' FROM dual UNION ALL SELECT 'ABDS' FROM dual UNION ALL SELECT 'BRBRD' FROM dual UNION ALL SELECT 'BRBRBD' FROM dual UNION ALL SELECT 'BRBDS' FROM dual UNION ALL SELECT 'BRBS' FROM dual UNION ALL SELECT 'B' FROM dual UNION ALL SELECT 'D' FROM dual UNION ALL SELECT 'S' FROM dual UNION ALL SELECT 'DS' FROM dual UNION ALL SELECT 'RB' FROM dual ) SELECT v , CASE WHEN REGEXP_LIKE(v, '^(A|B|AB)?(RB){0,}(D|DS)?$') THEN 'O' ELSE 'X' END ox FROM t ;