是否有可以将正则表达式存储为值的数据库?

前端之家收集整理的这篇文章主要介绍了是否有可以将正则表达式存储为值的数据库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一个可以将正则表达式存储为值的数据库.例如.像这样的事:
{:name => "Tim",:count => 3,:expression => /t+/},{:name => "Rob",:count => 4,:expression => /a\d+/},{:name => "Fil",:count => 1,:expression => /tt/},{:name => "Marc",:expression => /bb/}

所以我可以根据查询是否匹配表达式返回行/文档(例如“FIND rows WHERE”tt“=〜:expression”).并得到蒂姆和菲尔行作为结果.大多数数据库都可以完全相反(检查文本字段是否与正则表达式查询匹配).但不幸的是,mongo和postgres都不能做相反的事情.

附:或许我错了,有一些postgres或mongo的扩展允许我存储正则表达式?

Oracle数据库可以做到这一点.

示例查询:WHERE REGEXP_LIKE(first_name,’^ Ste(v | ph)en $’)

您想从列中选择正则表达式,请参阅下面的sql Fiddle示例以获取示例.

SQL Fiddle

选择Oracle数据库.

在架构窗口中执行以下命令:

CREATE TABLE regexp (name VARCHAR2(20),count NUMBER,regexp VARCHAR2(50));

INSERT INTO regexp VALUES ('Tim',3,'t+');
INSERT INTO regexp VALUES ('Rob',4,'a\d+');
INSERT INTO regexp VALUES ('Fil',1,'tt');
INSERT INTO regexp VALUES ('Marc','bb');
COMMIT;

执行sql语句,例如(正如你在问题中提到的那样):

SELECT * FROM regexp WHERE REGEXP_LIKE('tt',regexp);

产量:

NAME    COUNT   REGEXP
Tim     3       t+
Fil     1       tt

参考here.

摘抄:

Oracle Database implements regular expression support with a set of
Oracle Database sql functions and conditions that enable you to search
and manipulate string data. You can use these functions in any
environment that supports Oracle Database sql. You can use these
functions on a text literal,bind variable,or any column that holds
character data such as CHAR,NCHAR,CLOB,NCLOB,NVARCHAR2,and
VARCHAR2 (but not LONG).

还有一些需要考虑的信息:

A string literal in a REGEXP function or condition conforms to the
rules of sql text literals. By default,regular expressions must be
enclosed in single quotes. If your regular expression includes the
single quote character,then enter two single quotation marks to
represent one single quotation mark within the expression. This
technique ensures that the entire expression is interpreted by the sql
function and improves the readability of your code. You can also use
the q-quote Syntax to define your own character to terminate a text
literal. For example,you could delimit your regular expression with
the pound sign (#) and then use a single quote within the expression.

Note: If your expression comes from a column or a bind variable,then
the same rules for quoting do not apply.

请注意,没有名为RegEx的列类型,您需要在文本列中按原样保存字符串.

您还可以在约束检查和项目列中使用RegEx.

猜你在找的正则表达式相关文章