0

私は一生のように見えるこのコードを実行してきましたが、それを機能させることができないようです。

pattern = "\[([a-zA-Z0-9].*?)#([a-zA-Z0-9].*?)\]"
pattern_obj = re.compile(pattern, re.MULTILINE)
translation = pattern_obj.sub("<ol>\\1</ol>", translation)

ここでやろうとしているのは、いくつかのテキストを変更することです。

[ 
  # This is item number one in an ordered list. #

  # And this is item number two in the same list. #
]

の中へ:

<ol> 
#This is item number one in an ordered list. #
#And this is item number two in the same list. #
</ol>

基本的に、[ と ] の間のテキストをテキストのどこかで # で識別し、すべての内部テキストを同じに保ちながら[ を<ol>と ] に変更することになっています。</ol>誰でもアドバイスできますか?

前もって感謝します!

4

1 に答える 1

0

これはほとんどあなたが望むことをします:

>>> re.compile(r"\[([^\]]*)\]").sub("<ol>\\1</ol>", "b[#a]c")
'b<ol>#a</ol>c'

括弧の[^\]]後に ] を除くすべての文字を取ります。\[

を使用すると、次の#ようになります。

>>> re.compile(r"\[([^\]]*#[^\]]*)\]").sub("<ol>\\1</ol>", "b[#a]c")
'b<ol>#a</ol>c'
>>> re.compile(r"\[([^\]]*#[^\]]*)\]").sub("<ol>\\1</ol>", "b[gggg]c")
'b[gggg]c'

何かの.間に何かを見つけたい場合は、常に少し問題があります。

于 2013-05-28T16:26:56.983 に答える