加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:24:17 所属栏目:百科 来源:网络整理
导读:我正在寻找一个可以将正则表达式存储为值的数据库.例如.像这样的事: {:name = "Tim",:count = 3,:expression = /t+/},{:name = "Rob",:count = 4,:expression = /ad+/},{:name = "Fil",:count = 1,:expression = /tt/},{:name = "Marc",:expression = /bb/
我正在寻找一个可以将正则表达式存储为值的数据库.例如.像这样的事:
{:name => "Tim",:count => 3,:expression => /t+/},{:name => "Rob",:count => 4,:expression => /ad+/},{: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,'ad+');
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读