gpt4 book ai didi

postgresql - postgres子字符串转义开括号字符

转载 作者:行者123 更新时间:2023-11-29 13:47:30 25 4
gpt4 key购买 nike

我试图从 postgres 中的字符串字段返回括号内的所有字符:

这个函数似乎运行良好:

 select substring('foo bar [foobar] foo' from '%#"f%b#"%' for '#');
substring
-----------
foob

它不适用于左括号字符:

select substring('foo bar [foobar] foo' from '%#"[%]#"%' for '#');
substring
-----------

右括号工作正常,我试过反斜杠、双反斜杠、句号、单引号

最佳答案

你应该转义方括号,因为它们是元字符:

select substring('foo bar [foobar] foo' from '%#"#[%#]#"%' for '#');

substring
-----------
[foobar]
(1 row)

select substring('foo bar [foobar] foo' from '%#[#"%#"#]%' for '#');

substring
-----------
foobar
(1 row)

来自 the documentation :

In addition to these facilities borrowed from LIKE, SIMILAR TO supports these pattern-matching metacharacters borrowed from POSIX regular expressions:

...

  • A bracket expression [...] specifies a character class, just as in POSIX regular expressions.

关于postgresql - postgres子字符串转义开括号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991005/

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