PostgreSQL子字符串在方括号之间获取字符串 [英] PostgreSQL substring get string between brackets
本文介绍了PostgreSQL子字符串在方括号之间获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,说:
产品描述[白色]
我想使用PostgreSQL Substring函数从该字符串中提取括号内的任何内容(在本例中为 White
)。我可以使用 regexp_matches
使其工作,但这会返回我不想要的数组,除非我别无选择。
I want to extract anything inside the brackets (in this case White
) from that string, using the PostgreSQL Substring function. I can get this to work using regexp_matches
, but that returns an array which I don't want unless I have no other choice.
我尝试过:
-
substring('string'from'[(。)] ')
>>> NULL -
substring('string'from'\ [(。)\]')
>>> NULL -
substring('string'from'\\ [(。)\\]')
>>> NULL
substring('string' from '[(.)]')
>>> NULLsubstring('string' from '\[(.)\]')
>>> NULLsubstring('string' from '\\[(.)\\]')
>>> NULL
但这可行:
-
substring('W'(。)i]'的'string')
>>> h
substring('string' from 'W(.)i]')
>>> h
我在做什么错了?
推荐答案
(。)
仅匹配一个字符,但是您要在那里匹配多个字符。
(.)
only matches a single character, but you want to match multiple characters in there.
所以您需要(。+)
substring('Product Description [White]' from '\[(.+)\]')
这篇关于PostgreSQL子字符串在方括号之间获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文