【问题标题】:PostgreSQL substring get string between bracketsPostgreSQL 子字符串获取括号之间的字符串
【发布时间】:2016-03-24 03:06:39
【问题描述】:

我有一个字符串,比如说:

Product Description [White]

我想使用 PostgreSQL Substring 函数从该字符串中提取括号内的任何内容(在本例中为 White)。我可以使用regexp_matches 让它工作,但这会返回一个我不想要的数组,除非我别无选择。

我试过了:

  • substring('string' from '[(.)]') >>> 空
  • substring('string' from '\[(.)\]') >>> 空
  • substring('string' from '\\[(.)\\]') >>> 空

但这有效:

  • substring('string' from 'W(.)i]') >>> h

我做错了什么?

【问题讨论】:

    标签: postgresql pattern-matching substring


    【解决方案1】:

    我认为您想更新一些值,例如从薪级表中的等级工资或任何东西,只需使用以下查询

    更新 master.pay_scale SET Grade_pay= Case When description LIKE '%(%)%' Then (select substring(description from '((.+))')) else description End::numeric

    【讨论】:

      【解决方案2】:

      (.) 仅匹配单个字符,但您想匹配其中的多个字符。

      所以你需要(.+)

      substring('Product Description [White]' from '\[(.+)\]')
      

      【讨论】:

      • 只少了一个字符...非常感谢!
      • 您的回复对我帮助很大。谢谢!!
      • 如何为括号中的所有匹配项执行此操作
      • 这不适用于多个括号。 “产品描述 [White][Black] 将返回 White][Black... 正在寻找一种方法来改进它以满足我的需求。
      • 如果? substring('产品描述 [白色] [蓝色]' from '[(.+)]')
      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2012-07-15
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多