Search code examples
pythonregexregex-lookarounds

Regex Positive Lookahead Still Including Expression in Result


This is my regex expression.

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

I am trying to match

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

But it keeps returning with the 1) which I do not want it to like this.

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.

How do I only return the question?

EDIT: Since my bug couldn't be reproduced I am sharing the full code.

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

Solution

  • Your regex is fine, and is matching the right thing. The problem is that after you test it for a match, you use the original line instead of the matching portion. Do this instead:

    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