我正在尝试编写一个正则表达式,它只能匹配每个空格分隔的标记出现精确两次的行,无论顺序如何。
例如,以下整行应匹配:
1 1 2 2
100 10 10 100
A B B A
HELLO HELLO
以下行不应匹配:
hello hello hello
1 1 22
1001
虽然我能够使用正则表达式
(\d+)(?=.*(\1))
匹配给定行中的各个重复组,但我很难使用 ^$
匹配整行。我的猜测是,当我使用前瞻时,这会创建一个无限循环,我们不断地查看每个标记(包括重复)并期望稍后在字符串中重复,尽管我不确定如何解决这个问题。有任何想法吗?
谢谢!
在 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
v
)和部分(s
)进行分组输出:
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 |