Python 有没有办法在strip方法中使用regex模式

Python 有没有办法在strip方法中使用regex模式,python,regex,Python,Regex,我有一个这样的字符串列表 ... '43.990\none of the things we notice\nis that the headers all have\n\n296\n', '47.020\nthe word edit and\nbraces behind them,\n\n297\n', ... i.strip(r"[0-9.\\n]+") 我只需要上面列表中的文本,而不需要起始数字和\n。有没有像这样的正则表达式模式 ... '43.990\no

我有一个这样的字符串列表

...

'43.990\none of the things we notice\nis that the headers all have\n\n296\n',
'47.020\nthe word edit and\nbraces behind them,\n\n297\n',

...
i.strip(r"[0-9.\\n]+")
我只需要上面列表中的文本,而不需要起始数字和\n。有没有像这样的正则表达式模式

...

'43.990\none of the things we notice\nis that the headers all have\n\n296\n',
'47.020\nthe word edit and\nbraces behind them,\n\n297\n',

...
i.strip(r"[0-9.\\n]+")

这似乎不起作用。

您可以使用
re.sub
替换与正则表达式匹配的字符,并在行首和行尾加上锚:

i=re.sub('^[0-9.\n]+|[0-9.\n]+$,'',i,re.i)
输出(对于两个样本数据):

注意:我假设数据中的
\n
是一个实际的换行符,如果不是(它是文本字符串
\n
),则应该使用以下内容:

i=re.sub(r'^(?[0-9.]|\\n)+|(?:[0-9.]|\\n)+$,'',i,re.i)
输出:

one of the things we notice\nis that the headers all have
the word edit and\nbraces behind them,
我将使用:

inp=['43.990\n我们注意到的没有一件事\n是标题后面都有\n\n296\n','47.020\n单词edit和\n空格,\n\n297\n']
输出=[re.sub(r'\s*\d+(?:\.\d+?\s*','',x).strip()表示inp中的x]
打印(输出)
这张照片是:

['one of the things we notice\nis that the headers all have',
 'the word edit and\nbraces behind them,']

你能给我一个预期产出的样本吗?