0

我需要一个 varchar 中 2 个相同元音的正则表达式。

喜欢'linEbrEak'(但只有 2 个不多不少)

我想到了类似的东西:

'%[aeiouAEIOU]%[aeiouAEIOU]%'

但这给了我任何2个元音。我想要两个相同的。

我正在使用 SQL Server 2008。

这在正则表达式中甚至可能吗?非常感谢!

更多示例:

LeIpzIg
LOndOn

编辑:如果在正则表达式中不可能(也许太难)也没关系,我也对其他解决方案感到满意,但它们应该易于阅读,快速而简短。

4

2 回答 2

4

这不是漂亮或简短,但它很简单。

SELECT word
FROM tabl
WHERE
  -- assuming case sensitive based on your example
  (word LIKE '%[Aa]%[Aa]%' AND word NOT LIKE '%[Aa]%[Aa]%[Aa]%')
  OR
  (word LIKE '%[Ee]%[Ee]%' AND word NOT LIKE '%[Ee]%[Ee]%[Ee]%')
  OR
  (word LIKE '%[Ii]%[Ii]%' AND word NOT LIKE '%[Ii]%[Ii]%[Ii]%')
  OR
  (word LIKE '%[Oo]%[Oo]%' AND word NOT LIKE '%[Oo]%[Oo]%[Oo]%')
  OR
  (word LIKE '%[Uu]%[Uu]%' AND word NOT LIKE '%[Uu]%[Uu]%[Uu]%')

我突然想到,对于一个有两个一个元音和三个另一个元音的地方,您没有指定要做什么。这符合条件吗?如果不是(Alaska StatE PEak Park即使里面正好有 2 个 E,说也很糟糕),那么你可能想要这个:

SELECT word 
FROM tabl 
WHERE
  -- assuming case sensitive based on your example
  ( word LIKE '%[Aa]%[Aa]%'
    OR word LIKE '%[Ee]%[Ee]%'
    OR word LIKE '%[Ii]%[Ii]%'
    OR word LIKE '%[Oo]%[Oo]%'
    OR word LIKE '%[Uu]%[Uu]%' 
  )
  AND word NOT LIKE '%[Aa]%[Aa]%[Aa]%'
  AND word NOT LIKE '%[Ee]%[Ee]%[Ee]%'
  AND word NOT LIKE '%[Ii]%[Ii]%[Ii]%'
  AND word NOT LIKE '%[Oo]%[Oo]%[Oo]%'
  AND word NOT LIKE '%[Uu]%[Uu]%[Uu]%'
于 2013-09-30T21:08:06.347 回答
0

一种可能的解决方案

DECLARE @txt VARCHAR(100) = 'LeIpzIg'

DECLARE @vowels TABLE ( letter CHAR(1))
INSERT INTO @vowels VALUES ('a'),('e'),('i'),('o'),('u') 

;WITH cte AS (
    SELECT letter , LEN(@txt) - LEN(REPLACE(LOWER(@txt),letter,'')) as number_of_vowel
    FROM @vowels
    WHERE LOWER(@txt) LIKE '%'+ letter +'%'
) 
SELECT letter
FROM cte 
WHERE number_of_vowel >= 2
于 2013-09-30T19:35:20.693 回答