Pythonで次の置換を実現しようとしています。すべての html タグを {n} に置き換え、[tag, {n}] のハッシュを作成
元の文字列 -> " <h>
This is a string. </H><P>
This is another part. </P>
"置換された
テキスト -> "{0} This is a string. { 1}{2} これは別の部分です。{3}"
これが私のコードです。置換を開始しましたが、各オカレンスを連続して置換する最良の方法、つまり {0}、{1} などを見つけることができないため、置換ロジックに行き詰まっています。
import re
text = "<h> This is a string. </H><p> This is another part. </P>"
num_mat = re.findall(r"(?:<(\/*)[a-zA-Z0-9]+>)",text)
print(str(len(num_mat)))
reg = re.compile(r"(?:<(\/*)[a-zA-Z0-9]+>)",re.VERBOSE)
phctr = 0
#for phctr in num_mat:
# phtxt = "{" + str(phctr) + "}"
phtxt = "{" + str(phctr) + "}"
newtext = re.sub(reg,phtxt,text)
print(newtext)
誰かがこれを達成するためのより良い方法を手伝ってもらえますか? ありがとうございました!