Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在re中有两种模式_Python_Regex - Fatal编程技术网

Python 如何在re中有两种模式

Python 如何在re中有两种模式,python,regex,Python,Regex,假设a有一个单词列表,并想检查每个单词是否与模式“a?e”匹配,在哪里?可以是任何字母数字字符。另外,我想忽略包含“-”(破折号)的单词。我如何使用正则表达式来实现这一点 以下是我目前拥有的: for word in words: found = re.findall(r'([a]\w{1}[e])(^((?!(-)).)*$)', word) if found: print(word) 例如:['snake','take','want','sane','1',

假设a有一个单词列表,并想检查每个单词是否与模式“a?e”匹配,在哪里?可以是任何字母数字字符。另外,我想忽略包含“-”(破折号)的单词。我如何使用正则表达式来实现这一点

以下是我目前拥有的:

for word in words:
    found = re.findall(r'([a]\w{1}[e])(^((?!(-)).)*$)', word)
    if found:
        print(word)
例如:['snake','take','want','sane','1','snakke','s-ake']

我想过滤掉:
snake,take,sane


编辑:很抱歉,我的问题/代码不匹配

请使用
match
而不是
findall
match
将匹配整个字符串,而
findall
将查找与正则表达式匹配的子字符串

found = (w for w in words if re.match(r'g\wo', word))
你可以用

r'(?i)\b(?<!-)\w*a[a-z]e\w*\b(?!-)'

r'(?i)\b(?你能提供一些示例输入和期望的输出吗?在一个表达式中尝试这样做的理由是什么?过滤掉所有没有“-”的单词,然后如果它们通过第一个正则表达式,执行第二个正则表达式不是更简单吗?如果你使用
\w
[a-zA-Z0-9]
对于模式中的
,它将不匹配带破折号的单词。问题中的模式与代码示例中的模式不同。请重试