我试图在句子中的给定模式之前和之后获得所有单词。以下是我正在使用的代码。当然这并不完美,所以欢迎任何建议。我的问题在于,这适用于某些模式,但不适用于其他模式。例如,下面给出的模式不起作用,即。 re.match返回None。
sentence = "The agreements in this Section shall survive the resignation of the Administrative Agent, the L/C Issuer and the Swing Line Lender, the replacement of any Lender, the termination of the Aggregate Commitments and the repayment, satisfaction or discharge of all the other Obligations."
s = "commitments.*repayment"
rex = "(?i)((?:\\S+\\s+){0,30})\\b" + s + "\\b\\W*((?:\\S+\\s+){0,30})"
p = re.match(rex, sentence, re.IGNORECASE)
print p
我做错了什么?