首页 > 解决方案 > 当两者都存在时,如何返回较小的 OR 运算符正则表达式?

问题描述

这个正则表达式:

    import re
    string = 'I am a long string with many regexes matching'
    pattern = '.*(?:with|regexes)'
    print(re.findall(pattern, string))

返回

['I am a long string with many regexes']

我怎样才能让它返回:

['I am a long string with']

即当正则表达式的短版本和长版本都存在时,返回较短的字符串。

标签: pythonregex

解决方案


推荐阅读