>>> eq = '3x3+6x2+2x1+8x0'
>>> op = eq.split('+')
['3x3', '6x2', '2x1', '8x0']
>>> [o.split('x')[0] for o in op]
['3', '6', '2', '8']
>>> [o.split('x')[1] for o in op]
['3', '2', '1', '0']
(?=...)
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'.
(?<=...) Matches if the current position in the string is preceded by a match for
... that ends at the current position.
This is called a positive lookbehind
assertion. (?<=abc)def will find a
match in abcdef, since the lookbehind
will back up 3 characters and check if
the contained pattern matches.
还有另一种方法,不使用regex:
您可以使用positive look-ahead来匹配后面跟有其他内容的内容。要匹配系数,可以使用:
根据^{} 模块的文档:
对于指数,可以使用positive look-behind:
^{pr2}$同样,从文件中:
相关问题 更多 >
编程相关推荐