正则表达式:将整行与精确数量的重复标记相匹配

问题描述 投票:0回答:1

我正在尝试编写一个正则表达式,它只能匹配每个空格分隔的标记出现精确两次的行,无论顺序如何。

例如,以下整行应匹配:

1 1 2 2
100 10 10 100
A B B A 
HELLO HELLO

以下行不应匹配:

hello hello hello
1 1 22
1001

虽然我能够使用正则表达式

(\d+)(?=.*(\1))
匹配给定行中的各个重复组,但我很难使用
^$
匹配整行。我的猜测是,当我使用前瞻时,这会创建一个无限循环,我们不断地查看每个标记(包括重复)并期望稍后在字符串中重复,尽管我不确定如何解决这个问题。有任何想法吗? 谢谢!

regex regex-lookarounds
1个回答
0
投票

在 postgresql 中:


with recursive values as (
   select '1 1 2 2' as v union all
   select '100 10 10 100' as v union all
   select 'A B B A' as v union all 
   select 'HELLO HELLO' as v union all
   select 'hello hello hello' union all
   select '1 1 22' union all
   select '1001'
), nrs as (
   select 1 as x
   union all
   select x+1 from nrs where x<=10
)
select 
    v, 
    length(v)-length(replace(v,' ',''))+1 nrOfItems,
    split_part(v,' ',x) as s,
    count(*) as c
from values
cross join nrs
where nrs.x <= length(v)-length(replace(v,' ',''))+1
group by v,s
having not count(*)<>2
order by v
  • 与 nrs 的交叉连接在空间上分离后生成零件。
  • 然后对文本(
    v
    )和部分(
    s
    )进行分组
  • 计数不等于2

输出:

v nrofitems s c
100 10 10 100 4 10 2
100 10 10 100 4 100 2
1 1 2 2 4 2 2
1 1 2 2 4 1 2
1 1 22 3 1 2
ABBA 4 A 2
ABBA 4 B 2
你好你好 2 你好 2
© www.soinside.com 2019 - 2024. All rights reserved.