Datasette插件,增加使用Rust正则表达式引擎执行匹配的自定义SQL函数
项目描述
datasette-rure
Datasette插件,增加使用Rust正则表达式引擎执行匹配的自定义SQL函数
在Datasette相同的环境中安装此插件以启用regexp()
SQL函数。
$ pip install datasette-rure
此插件基于David Blewett的rure-python库。
使用regexp()测试正则表达式
您可以通过这种方式测试一个值是否匹配正则表达式:
select regexp('hi.*there', 'hi there')
-- returns 1
select regexp('not.*there', 'hi there')
-- returns 0
您还可以使用SQLite的自定义语法来运行匹配
select 'hi there' REGEXP 'hi.*there'
-- returns 1
这意味着您可以根据正则表达式匹配选择行 - 例如,选择标题以E或F开头的每一篇文章
select * from articles where title REGEXP '^[EF]'
试试这个: REGEXP交互式演示
使用regexp_match()提取组
您可以使用regexp_match()
来提取模式的捕获子集。
select regexp_match('.*( and .*)', title) as n from articles where n is not null
-- Returns the ' and X' component of any matching titles, e.g.
-- and Recognition
-- and Transitions Their Place
-- etc
使用两个参数调用时,这将返回第一个括号匹配。您可以使用三个参数来指定您想要提取的匹配项
select regexp_match('.*(and)(.*)', title, 2) as n from articles where n is not null
对于无效输入,例如没有捕获组的模式,函数将返回null
试试这个: regexp_match()交互式演示
使用regexp_matches()一次性提取多个匹配项
regexp_matches()
函数可用于从单个字符串中提取多个模式。结果作为JSON数组返回,然后可以使用SQLite的JSON函数进一步处理。
第一个参数是具有命名捕获组的正则表达式。第二个参数是要匹配的字符串。
select regexp_matches(
'hello (?P<name>\w+) the (?P<species>\w+)',
'hello bob the dog, hello maggie the cat, hello tarquin the otter'
)
这将返回一个JSON对象列表,每个对象代表原始正则表达式中的命名捕获
[
{"name": "bob", "species": "dog"},
{"name": "maggie", "species": "cat"},
{"name": "tarquin", "species": "otter"}
]
试试这个: regexp_matches() 交互式演示
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源代码分发
此版本没有可用的源代码分发文件。请参阅有关 生成分发存档 的教程。
构建分发
datasette_rure-0.3-py3-none-any.whl (7.5 kB 查看哈希值)