我有一个字符串格式:
(header1:content1(note1, note2),content2(note3),content3)-(header2:content)-(header3)
现在我想删除所有内容,我想要的输出是
(header1)-(header2)-(header3)
我该怎么做?我尝试了一些正则表达式,但输出不正确。你知道吗
更新1:header
、content
和note
可以包含除(
和)
之外的任何字符。你知道吗
更新2: @阿德史密斯解决了我原来的问题。现在我的字符串格式如下:
normalcontent1-(header1:content1(note1, note2),content2(note3),content3)-(header2:content)-normalcontent2-(header3)
预期产量:
normalcontent1-(header1)-(header2)-normalcontent2-(header3)
请注意,如果
header
包含:
,则此操作将失败,因此如果这些情况看起来不正确,您可能需要手动解析它们。我没有一个很好的解决方案,如果我不能从内容中划出标题,对不起。如果内容不能有:
,你可以只做split(":")[:-2]
,但是如果头和内容都可以包含一个:
,那么就不可能(以编程的方式)知道头的结束和内容的开始。你知道吗以下是pyparsing的示例:
印刷品:
如果正确定义语法,解析器将是比正则表达式更健壮的解决方案。你知道吗
相关问题 更多 >
编程相关推荐