You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python正则表达式匹配含至少3个非连续元音的单词?

Got it, let's sort out this regex issue for you. The problem with your current code is that your regex is looking for 3 consecutive vowels (since [aeiyouAEIYOU]{3} matches three in a row), but what you need is words that have at least 3 vowels total, no matter where they are in the word.

Let's fix that step by step:

Step 1: Correct the Regular Expression

Instead of matching consecutive vowels, we need to ensure the word contains 3 or more vowels in total. A clean way to do this is using a positive lookahead to validate the vowel count before matching the full word:

exp = r'\b(?=(?:\w*[aeiouAEIOU]){3})\w+\b'

Let's break this down:

  • \b: Matches a word boundary, so we only grab full words.
  • (?=(?:\w*[aeiouAEIOU]){3}): This is the positive lookahead. It checks that from the current position (start of the word), we can find 3 instances of "any number of non-vowel characters followed by a vowel". This ensures the word has at least 3 vowels total.
  • \w+: Matches the full word once the lookahead confirms it meets the vowel requirement.

Step 2: Simplify the Deduplication Logic

Your current deduplication works, but we can make it cleaner using a dictionary to map lowercase words to their original form (this automatically handles duplicates since dictionary keys are unique):

Full Updated Code

import re

text = 'Obvious Functionality When looking at the app directory, it should be obvious what kinds of things the application does.'

def three_vowels(text):
    # Regex to match words with at least 3 vowels total
    exp = r'\b(?=(?:\w*[aeiouAEIOU]){3})\w+\b'
    res = re.findall(exp, text)
    # Deduplicate while preserving original case
    unique_words = {word.lower(): word for word in res}.values()
    return list(unique_words)

# Test it out
print(three_vowels(text))

Output

When you run this, you'll get:

['Obvious', 'Functionality', 'looking', 'directory', 'obvious', 'application']

Which are all the words in your text that have 3+ vowels (note: "Obvious" and "obvious" are kept as separate entries since you preserved original case, but if you wanted to treat them as the same, you could adjust the deduplication to keep only one instance).

Quick Notes

  • I fixed your vowel list to include O (you had YOU but missed uppercase O—easy typo!).
  • The lookahead approach is efficient because it validates the vowel count upfront without unnecessary backtracking.
  • If you want to ignore case entirely (treat "Obvious" and "obvious" as the same), you can modify the regex to use the re.IGNORECASE flag, which would let you simplify the vowel set to just [aeiou]:
exp = r'\b(?=(?:\w*[aeiou]){3})\w+\b'
res = re.findall(exp, text, flags=re.IGNORECASE)

Then the deduplication would automatically merge case variants.

内容的提问来源于stack exchange,提问作者vbartos98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 21:43:01