postgres子字符串转义开括号字符
postgres substring escape open bracket character
我正在尝试 return 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)
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.
我正在尝试 return 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)
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.