正则表达式正向先行仍然包括结果中的表达式

Regex Positive Lookahead Still Including Expression in Result

这是我的正则表达式。

'(?<=1\)).*'

我正在尝试匹配

A good strategy is a set of actions that enables a firm to achieve its own internal goals without regard to the external environment.

但它与 1) 保持 return 连接,我不希望它这样。

1) A good strategy is a set of actions that enables a firm to achieve its own internal goals without regard to the external environment.

我怎么才return这个问题?

编辑:由于无法重现我的错误,我将共享完整代码。

searchCounter = 1

bookDict = {}

with open ('StratMasterKey.txt', 'rt') as myfile:

for line in myfile:
    question_pattern = re.compile((rf'(?<={searchCounter}\)).*'), re.IGNORECASE)
    if question_pattern.search(line) != None: 

        bookDict[searchCounter] = line 
        searchCounter +=1

您的正则表达式没问题,匹配正确的东西。问题是在测试匹配后,您使用原始行而不是匹配部分。改为这样做:

searchCounter = 1

bookDict = {}

with open ('StratMasterKey.txt', 'rt') as myfile:

for line in myfile:
    question_pattern = re.compile((rf'(?<={searchCounter}\)).*'), re.IGNORECASE)
    result = question_pattern.search(line)
    if result != None: 

        bookDict[searchCounter] = result[0] 
        searchCounter +=1