在Python中使用正则表达式用HTML标记替换wiki标记

2024-09-27 02:22:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到了几个用wiki标记(==,==,--,--,#等)格式化的文本文档,我不得不用HTML标记(–,-,<;>;等)来重新格式化它们。现在,如果格式化成功,它必须返回True,如果格式化失败,它必须返回False。到目前为止,我的情况是:

def wiki_markup ():
    for () in range ()
    sub('--', '&ndash;')
    sub('---', '&mdash;')
    sub('''(.+)''', '<i>(.+)</i>')
    sub("'''(.+)'''", '<b>(.+)</b>')
    sub("==(.+)==", "<h1>(.+)</h2>")
    sub("#(.+)#", "<li>(.+)</li>")
    sub("*(.+)*","<li>(.+)</li>")

现在我知道我需要输入一些代码,如果它成功了,那么就打印true,否则就打印false,但是我真的被困在了这一部分。我甚至不知道我到目前为止所做的是否在正确的轨道上。我将发布一个我试图重新格式化的示例:

== Early years ==

Rachel Chiesley was one of ten children born to John Chiesley of [[Dalry, Edinburgh|Dalry]] and Margaret Nicholson. The marriage was unhappy and Margaret took her husband to court for [[alimony]]. She was awarded 1,700 [[Merk (coin)| merks]] by [[George Lockhart (advocate)| Sir George Lockhart of Carnwath]], the [[Lord President of the Court of Session]]. Furious with the result, John Chiesley shot Lockhart dead on the [[Royal Mile]] in [[Edinburgh]] as he walked home from church on [[Easter Sunday]], 31 March 1689.{{Sfn | Macaulay | 2009 | pp = 23–24}} The assailant made no attempt to escape and confessed at his trial, held before the [[Lord Provost]] the next day. Two days later he was taken from the [[Heart of Midlothian (Royal Mile)|Tolbooth]] to the Mercat Cross on the High Street. His right hand was cut off before he was hanged, and the pistol he had used for the murder was placed round his neck.{{Sfn | Macaulay | 2009 | pp = 29–30}} Rachel Chiesley's birthday is unknown but she was baptised on 4 February 1679 and was probably born shortly before then, making her about ten years old at the time of her father's execution.{{Sfn | Macaulay | 2009 | p = 19}}

它应该以黑体字的形式出现在标记的地方等等

有人能帮忙吗?你知道吗


Tags: andoftheto标记foronli

热门问题