如何在正则表达式中使条件惰性化?

问题描述 投票:0回答:1

考虑这段代码:

import re
text = 'Apple Inc is an American computer and consumer electronics company famous for creating the iPhone, iPad and Macintosh computers. It was previously known as Apple Computer Inc. Apple Inc is one of the largest companies globally with a market cap of over 2 trillion dollars. It has different products.'

re.sub('\s*It was previously known as ' + '[\w\d@_!#$%^&*()<>?/\|}{~:. ]+ ' + "(?=(. It|Apple Inc))",'', text)

根据我的理解和文档,

(. It|Apple Inc)
尝试第一部分。如果它找到这个模式,它不会移动到第二个模式 (
Apple Inc
)。当我运行这段代码时,输出是:

'Apple Inc is an American computer and consumer electronics company famous for creating the iPhone, iPad and Macintosh computers.Apple Inc is one of the largest companies globally with a market cap of over 2 trillion dollars. It has different products.'

表示直接移动到第二个模式。你能解释一下这些条件在正则表达式中是如何工作的吗?我怎样才能让它变得懒惰?

python nsregularexpression
1个回答
0
投票

由于标示空间

re.sub('\s*It was previously known as ' + '[\w\d@_!#$%^&*()<>?/\|}{~:. ]+ ' + "(?=(. It|Apple Inc))",'', text)
                                                                         ^

第一个选项需要匹配“空格、任意字符(点)、空格、'It'”,但第一个空格不存在。

© www.soinside.com 2019 - 2024. All rights reserved.