从正则表达式匹配的字符串中删除换行符

Remove newlines from a regex matched string

我有如下字符串:

Financial strain:       No\n?Food insecurity:\nWorry:                            No\nInability:                        No\n?Transportation needs:\nMedical:                          No\nNon-medical:                      No\nTobacco Use\n?Smoking status:                  Never Smoker\n?

我想先匹配感兴趣的 substring/sentence(即以“Food insecurity”开头并以“\n?”结尾的句子)然后删除这句话中除最后一个以外的所有换行符即问号前的那个。

我已经能够将句子 w/o 它的最后一个换行符和问号与正则表达式 (Food insecurity:).*?(?=\n\?) 匹配,但我很难删除匹配句子的前 2 个换行符和 return整个预处理字符串。有什么建议吗?

您可以将 re.sub 与回调函数一起使用:

inp = "Financial strain:       No\n?Food insecurity:\nWorry:                            No\nInability:                        No\n?Transportation needs:\nMedical:                          No\nNon-medical:                      No\nTobacco Use\n?Smoking status:                  Never Smoker\n?"
output = re.sub(r'Food insecurity:\nWorry:                            No\nInability:                        No(?=\n\?)', lambda m: m.group().replace('\n', ''), inp)
print(output)