1

嗨,遇到特定问题的人。我正在使用 python 的正则表达式来更改标记源以输出 html 格式。

标记来源:

[ 
# sometextsometextsometextsometextsometextsometext.  #

# sometextsometextsometextsometextsometextsometextsometextsometext
sometextsometextsometextsometextsometextsometext. #
]


[
hello i am a normal paragraph.
]

所需的输出:

<ol> 
<li> sometextsometextsometextsometextsometextsometext.  </li>

<li> sometextsometextsometextsometextsometextsometextsometextsometext
sometextsometextsometextsometextsometextsometext. </li>
</ol>

<p>
hello i am a normal paragraph.
</p>
4

1 回答 1

1
import re
with open('mk.txt') as f:
    with open('newmk.txt','w+') as g:
        text = f.read()
        SquareGroups = re.findall(r'\[(?:.|\n)+?\]',text)
        for group in SquareGroups:
            if '#' in group: #must be ol
                group = group.replace('[','<ol>')
                group = group.replace(']','</ol>')
                group = re.sub('#(?= ?\w)','<li>',group)
                group = re.sub('(?<=[\w ])#','</li>',group)
            else:
                group = group.replace('[','<p>')
                group = group.replace(']','</p>')
            g.write(group)
            g.write('\n') #optional, just makes the output look 'nicer'

将您的输入mk.txt转换为以下文本newmk.txt

<ol>
<li> sometextsometextsometextsometextsometextsometext.  </li>

<li> sometextsometextsometextsometextsometextsometextsometextsometext
sometextsometextsometextsometextsometextsometext. </li>
</ol>
<p>
hello i am a normal paragraph.
</p>
于 2013-05-08T01:17:06.260 回答