regex - 正则表达式的最大长度是多少?
问题描述
desc
在 PostgreSQL 中,如果字段包含任何禁止的单词,我想排除行。
items
:
| id | desc |
|----|------------------|
| 1 | apple foo cat bar|
| 2 | foo bar |
| 3 | foocatbar |
| 4 | foo dog bar |
禁用词列表存储在另一个表中,目前它有 400 个单词要检查。
forbidden_word_table
:
| word |
|---------|
| apple |
| boy |
| cat |
| dog |
| .... |
SQL查询:
select id, desc
from items
where
desc !~* (select '\y(' || string_agg(word, '|') || ')\y' from forbidden_word_table)
我正在检查是否desc
与正则表达式不匹配:
desc !~* '\y(apple|boy|cat|dog|.............)\y'
结果:
| id | desc |
|----|------------------|
| 2 | foo bar |
| 3 | foocatbar |
** 3rd 不被排除,因为cat
它不是一个单词
我forbidden_word_table
的行数可能会增长,上面的正则表达式将成为一个非常冗长的表达式。
正则表达式是否有最大长度限制(以字节或字符为单位)?我担心如果forbidden_word_table
不断增长,我的正则表达式匹配方法将不起作用。
解决方案
看来,Wiktor Stribiżew关于“灾难性的回溯”是正确的。
SELECT *
FROM items i
WHERE NOT i."desc" ILIKE ANY
(
SELECT '%' || word || '%'
FROM forbidden_word_table
);
推荐阅读
- checkbox - 谷歌表格 - 当框被放置在广泛的特定单元格中时,只允许选中一个复选框
- c# - 如何在另一个线程中继续 TaskCompletionSource<>?
- oracle-apex - 在顶点 oracle 中关闭模式对话框后刷新项目
- python - 更新 anaconda 失败 - 找不到入口点
- angular - 使用应用程序组件从角度服务中获取变量
- python - sh: line 0: cd: PRET: No such file or directory error in Python
- algorithm - 使用 Big Theta 表达式进行简化
- xml - 有命名空间时的 UPDATEXML
- javascript - if 语句将 true 视为 false
- arrays - 读取列表中的数组部分