English 中文(简体)
Regex:Match小组,多次时间和重新定位
原标题:Regex: Match group, multiple time and reset position

I would like to be able to return all canadian postalcode which can be find in using the following regex(migth have a space like: A0A 0A0 or A0A0A0) regex [A-Z]d[A-Z]s*d[A-Z]d The problem is when there is more than 1 match that a part of it it in . I ve played with capturing group (too much) and i m more lost then at the start, oh well.

  • 鉴于图谋: 缩略语

    • I would expect the following match
      • A0A 0A0 (ok)
  • 鉴于浮雕:SOMETEXTB1A0A0OTHERSTUFFB2A 0B0OTHER

    • I would expect the following match:
      • B1A0A0 (ok)
      • A0A0A0 (not found)
      • B2A 0B0 (not found)

The regex101

这里是我为后格雷斯克的考验。

with barcodes as(
    select * 
    from
        (values
         ( AAAC9B92WLEDH0G5R0Z0|2024-01-01T01:59:39.379-05 )
         , (  SOMETEXTJ9H5G0L2J0OTHERSTUFF )
        )b(barcode)
)
select * 
from barcodes  b
left join lateral  (    
    SELECT UNNEST(
        REGEXP_MATCHES(
            b.barcode
            ,  [A-Z]d[A-Z]s*d[A-Z]d 
            ,  g 
        )
    ) match
)t on true
问题回答

你们可以直接取得你想要的结果,尽管它需要一定程度的工作。 我们不是试图与整个reg子相匹配,而是仅仅与reg首的特性相对应,而是用head头 assertion言来与reg的平衡相匹配。 这确保了我们找到能够与监管机构相匹配的所有地点,不论它们是否重叠。 因此,我们首先使用这一规定:

[A-Z](?=d[A-Z]s*d[A-Z]d)

我们首先在regexp_count上使用,以了解在座标上有多少对应,并使用<代码>generate_series<<>/code>生成代表每个人的序列号。 然后,我们使用<代码>regexp_instr,以找到每一方在座标上的起始位置。 最后,我们使用<代码>regexp_substr,开端职位和原封顶,以提取对应物:

with barcodes as (
    select * 
    from
        (values
         ( SOMETEXTB1A0A0A0OTHERSTUFFB2A 0B0OTHER )
         , (  SOMETEXTJ9H5G0L2J0OTHERSTUFF )
         , (  SOMETEXTA0A 0A0OTHERSTUFF  )
        )b(barcode)
), counts as (
select *
from barcodes b
left join lateral (
  select generate_series as n
  from generate_series(1, regexp_count(barcode,  [A-Z](?=d[A-Z]s*d[A-Z]d) ))
) c on true
),
positions as (
select barcode, regexp_instr(barcode,  [A-Z](?=d[A-Z]s*d[A-Z]d) , 1, n) as start
from counts
)
select barcode, regexp_substr(barcode,  [A-Z]d[A-Z]s*d[A-Z]d , start) as postcode
from positions

产出:

barcode                                 postcode
SOMETEXTB1A0A0A0OTHERSTUFFB2A 0B0OTHER  B1A0A0
SOMETEXTB1A0A0A0OTHERSTUFFB2A 0B0OTHER  A0A0A0
SOMETEXTB1A0A0A0OTHERSTUFFB2A 0B0OTHER  B2A 0B0
SOMETEXTJ9H5G0L2J0OTHERSTUFF            J9H5G0
SOMETEXTJ9H5G0L2J0OTHERSTUFF            H5G0L2
SOMETEXTJ9H5G0L2J0OTHERSTUFF            G0L2J0
SOMETEXTA0A 0A0OTHERSTUFF               A0A 0A0

说明一单独列出步骤,以明确这项工作如何进行,你可以很容易地将一些步骤结合起来,以减少行动次数(见关于相关dle的第二次询问)。 如果希望,你也可以将成果汇总到一个阵列。

Demo on dbfiddle.uk





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签