当您想使用正则表达式从 HTML 中选择一些文本并且潜在选择之后发生的内容很重要时,我想您必须执行以下操作:
selected = re.findall(r'<a (.*?) >About', text)
显然这不起作用,但是正确的方法是什么?
最佳答案
您想要使用前瞻断言。 From the python docs :
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
就您而言:
re.findall(r'<a (.*?)(?= *>About)', text)
关于python - 看看潜在的选择正则表达式之后会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111522/