我想进行regexpr模式匹配,但我做不到。请帮忙。
我想评估将要插入DB中的值。我要执行以下检查。
仅允许使用四个特殊字符和字母。不允许使用其他特殊字符。
四个字符是const User = bookshelf.Model.extend('User', {
tableName: 'users',
org: function () {
return this.belongsToMany('Org', 'Member')
}
})
点,.
连字符-
撇号和'
空格
我的价值将满足以下条件。
,......
,----------
。例如,这些是有效的:
''''''''
ORACLE
ORA..C L--E
''..O
这些是无效的:
O--...'''
........
''''''''''
--------
ORACLE$
答案 0 :(得分:2)
使用正则表达式^( +|[A-Z '.-]*[A-Z][A-Z '.-]*)$
来匹配仅包含以下任意一项的字符串:
+所有空格字符;或
[A-Z '.-]*[A-Z][A-Z '.-]*
赞:
Oracle设置:
CREATE TABLE data (
value VARCHAR2(100),
CONSTRAINT value__alpha_or_special_chr CHECK ( REGEXP_LIKE( value, '^( +|[A-Z ''.-]*[A-Z][A-Z ''.-]*)$' ) )
);
插入有效数据:
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
无效数据失败:
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
查询:
SELECT * FROM data;
输出:
| VALUE | | :----------- | | ORACLE | | ORA..C L--E | | '''..O | | O--...'''' | | |
db <>提琴here