0

我正在尝试制定一个正则表达式(在 Python 中运行),它通过一个单词并且只需要找到不包含 2 个相邻元音的单词。例如:

me - would match
mee - would not match
meat - would not match
base - would match
basketball - would match

我在这里迷路了,因为我不知道如何检查不存在的东西?

谢谢您的帮助

4

2 回答 2

4
import re

r = re.compile("[aeiou][aeiou]")
m = r.search("me")   # => None
m = r.search("mee")  # => Matcher
m = r.search("meat") # => Matcher
m = r.search("base") # => None

对于所有不匹配的情况也是如此if not mTrue

于 2012-08-13T13:09:19.957 回答
3
m = re.match(r"(?:[^euioa]|[euioa](?![euioa]))*$", word)

@Tichodroma 的答案更简单,因此如果您以后可以在代码中否定匹配,则应该更可取,即,只需写下if not m您将if m使用此解决方案编写的位置。

于 2012-08-13T13:28:00.353 回答