gpt4 book ai didi

匹配 postgresql bytea 的正则表达式

转载 作者:行者123 更新时间:2023-11-29 12:14:58 25 4
gpt4 key购买 nike

在 PostgreSQL 中,有一个名为 bytea 的 BLOB 数据类型。它只是一个字节数组。

bytea 字面量以下列方式输出:

'\\037\\213\\010\\010\\005`Us\\000\\0001.fp3\'\\223\\222%'

参见 PostgreSQL docs格式的完整定义。

我正在尝试构建一个 Perl regular expression它将匹配任何这样的字符串。
它还应匹配标准 ANSI SQL 字符串文字,例如 'Joe' , 'Joe''s Mom' , 'Fish Called ''Wendy'''
它还应匹配反斜杠转义变体:'Joe\'s Mom' , .

第一种方法(如下所示)仅适用于某些 bytea 表示。

s{ '               # Opening apostrophe
(?: # Start group
[^\\\'] # Anything but a backslash or an apostrophe
| # or
\\ . # Backslash and anything
| # or
\'\' # Double apostrophe
)* # End of group
' # Closing apostrophe
}{LITERAL_REPLACED}xgo;

对于其他的(较长的,带有许多转义的撇号,Perl 给出这样的警告:

Complex regular subexpression recursion limit (32766) exceeded at ./sqa.pl line 33, <> line 1.

所以我正在寻找一个更好的(但仍然基于正则表达式的)解决方案,它可能需要一些正则表达式炼金术(避免反向引用和所有)。

最佳答案

好的,感谢 Leon 和 hobbs,这是我能找到的最好的解决方案。

注意:这不是我要找的解决方案!对于一些长字符串,它仍然使 Perl 失败并发出警告“超出递归限制(32766)”。 (尝试将 400k 随机字节填充到 bytea 字段中,然后使用 pg_dump --inserts 导出)。

但是,它匹配大多数 bytea 字符串(因为它们出现在 SQL 代码和服务器日志中)和 ANSI SQL 字符串文字。例如:

'\014cS\0059\036a4JEd\021o\005t\0015K7'
'\\037\\213\\010\\010\\005`Us\\000\\0001.fp3\'\\223\\222%'
' Joe''s Mom friend\'s dog is called \'Fluffy'''

这是正则表达式:

m{ 
' # opening apostrophe
(?> # start non-backtracking group
[^\\']+ # anything but a backslash or an apostrophe, one or more times
| # or
(?: # group of
\\ \\? [0-7]{3} # one or two backslashes and three octal digits
)+ # one or more times
| # or
'' # double apostrophe
| # or
\\ [\\'] # backslash-escaped apostrophe or backslash
)* # end of group
' # closing apostrophe
}x;

关于匹配 postgresql bytea 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2355385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com