postgresql Postgres正则表达式问题

bhmjp9jg  于 2023-03-29  发布在  PostgreSQL
关注(0)|答案(1)|浏览(153)

我有一个表,其中有一个字段(字符串),上面有一个完整的Swagger文本。
字段示例

swagger: '2.0'
info:
  title: "Values"
  version: 2.4.0
  description: "Lorem iosum."
  license:
    name: Copyright
    url: 'https://lorem.com/'
  x-catalogation:
    ba: "Execution"
    bd: "Loans"
    bdd: "Account" 
schemes:
  - "https"
basePath: "/v2/values"
consumes:
  - "application/json"
produces:
  - "application/json"
securityDefinitions:
  JWTProfile:
    type: "oauth"
    description: 'OGT'
    flow: "implicit"
    scopes:
      valueid.read: "lorem"
      valuelist.read: "lorem"
      valuetran.read: "lorem"
      value: "lorem"
      vlimit: "lorem"
      valuestat.read: "lorem"
    authorizationUrl: "$(authorization-url)"
    x-tokenIntrospect:
      url: "$(sca-security-url)"
paths:...

查询的期望结果必须是valueid.read valuelist.read valuetran.read value vlimit valuestat.read
我尝试了这个查询select unnest (regexp_matches(api_spec, E'\"([a-z]+\\.[a-z]+)\"', 'g')) as scopes from apis
但我只检索这个范围valueid.read valuelist.read valuetran.read valuestat.read
我在一个角落里,有人能帮我吗?

2lpgd968

2lpgd9681#

**免责声明:**我建议将此答案视为纯粹的练习:如果你需要解析YAML,最好使用一些支持YAML格式解析的库的编程语言。

假设scope的子元素的缩进总是相同的,并且它们都没有任何子元素,你可以用下面的查询提取作用域子元素的名称:

select
    unnest (regexp_matches (t2.val2, '\S+(?=:)', 'g')) as children
from
    (
        select
            unnest (regexp_matches (api_spec, 'scopes:\n((?:      [^\n]+\n)+)')) as val2
        from
            apis
    ) as t2

示例的预期输出:
| 儿童|
| --------------|
| valueid.read |
| valuelist.read |
| valuetran.read |
| 价值|
| vlimit|
| valuestat.read |
演示here

相关问题