0

I want to do the regexpr pattern matching and I couldn't. Please help.

I want to evaluate the value that is going to be inserted in DB. I want to perform a check as below.

Only four special characters and alphabeticals are allowed. other special characters are not allowed.

Four characters are . dot, - Hyphen ' apostrophe and space

My value will have meet below condition.

  • It contains only alphabets.
  • It contains combination of alphabets and above special characters.
  • it contains only all spaces.
  • Except for spaces it should not be simply special characters as ......, ----------, ''''''''.

For example these are valid:

  • ORACLE
  • ORA..C L--E
  • ''..O
  • O--...'''

and these are invalid:

  • ........
  • ''''''''''
  • --------
  • ORACLE$
  • ORACL#E
MT0
  • 143,790
  • 11
  • 59
  • 117
CHANDRU S
  • 147
  • 2
  • 13

1 Answers1

2

Use the regular expression ^( +|[A-Z '.-]*[A-Z][A-Z '.-]*)$ to match a string consisting of only either:

  •  +
    all space characters; or
  • [A-Z '.-]*[A-Z][A-Z '.-]*
    zero-or-more alphabetical or special characters then one alphabetical character and then zero-or-more alphabetical or special characters.

Like this:

Oracle Setup:

CREATE TABLE data (
  value VARCHAR2(100),
  CONSTRAINT value__alpha_or_special_chr CHECK ( REGEXP_LIKE( value, '^( +|[A-Z ''.-]*[A-Z][A-Z ''.-]*)$' ) )
);

Insert valid data:

INSERT INTO data ( value )
SELECT q'!ORACLE!'       FROM DUAL UNION ALL
SELECT q'!ORA..C  L--E!' FROM DUAL UNION ALL
SELECT q'!'''..O!'       FROM DUAL UNION ALL
SELECT q'!O--...''''!'   FROM DUAL UNION ALL
SELECT q'!     !'        FROM DUAL

Invalid data fails:

INSERT INTO data ( value )
SELECT q'!''''''''''!' FROM DUAL;
ORA-02290: check constraint (FIDDLE_TRJCYMMSYLSIPALCEYXD.VALUE__ALPHA_OR_SPECIAL_CHR) violated
INSERT INTO data ( value )
SELECT q'!--------!'   FROM DUAL;
ORA-02290: check constraint (FIDDLE_TRJCYMMSYLSIPALCEYXD.VALUE__ALPHA_OR_SPECIAL_CHR) violated
INSERT INTO data ( value )
SELECT q'!ORACLE$!'    FROM DUAL;
ORA-02290: check constraint (FIDDLE_TRJCYMMSYLSIPALCEYXD.VALUE__ALPHA_OR_SPECIAL_CHR) violated
INSERT INTO data ( value )
SELECT q'!ORACL#E!'    FROM DUAL;
ORA-02290: check constraint (FIDDLE_TRJCYMMSYLSIPALCEYXD.VALUE__ALPHA_OR_SPECIAL_CHR) violated

Query:

SELECT * FROM data;

Output:

| VALUE        |
| :----------- |
| ORACLE       |
| ORA..C  L--E |
| '''..O       |
| O--...''''   |
|              |

db<>fiddle here

MT0
  • 143,790
  • 11
  • 59
  • 117
  • But error is there in the result given by this query. it allows the wrong value "ORACLE)" to get inserted. Please suggest. – CHANDRU S Feb 19 '19 at 04:45
  • ORACLE* and ORACLE, and ORACLE( and ORACLE) are allowed here. Please help to correct it and update the answer. – CHANDRU S Feb 19 '19 at 05:22
  • @CHANDRUS Updated - swap `''-.` to `''.-` in both places of the regular expression. – MT0 Feb 19 '19 at 10:59
  • @ MT0 - It is working fine now. could you please tell what is the reason of swapping the metacharacter? So it will be helpful. Or please share the links where we can find more about these kind of checks. – CHANDRU S Feb 20 '19 at 02:53