2016-06-30 21 views
2

我當前的查詢是:我如何找到只有2個_的行在Postgres中?

SELECT * FROM "Questions" WHERE "questionText" ~ '[ $][_][$ ]' AND "status" != 'inactive'; 

這將返回:

  • 吉爾很震驚地發現,她沒有_在測試的答案。
  • 布魯克林驕傲地站在她的頭頂上。
  • 健康的飲食是一個_主意。
  • 我_觀看_電影。

我只想返回像2號(或更多)_第二和第四護理項目。我不想讓多個____彼此相鄰。

回答

1

這樣的事情呢?

WITH questions (questionText, status) AS (
VALUES 
    ('Jill was shocked to find that she _ none of the answers in the test.','active'), 
    ('Brooklyn stood joyously _ her crown proudly _ top _ her head','active'), 
    ('A healthy diet is a _ idea.','active'), 
    ('I _ watch a _ movie.','active') 
) 

SELECT questionText 
FROM questions 
WHERE array_length(regexp_split_to_array(questionText,'[ $][_][$ ]'),1) > 2 
AND status != 'inactive'; 

輸出

enter image description here

+0

@Shamoon'WHERE questionText〜「(([^ _] + | ^)_([^ _] + | $)){2,} '' – Abelisto

+0

讓它成爲答案,我會接受它 – Shamoon

相關問題