ユーザーがHTMLをフォーマットするためのカスタム構文を作成しようとしています
ユーザーは次のように入力できます。
**some text here**
the some more text down here
**Another bunch of stuff**
then some other junk
and I get:
<h1>some text here</h2>
<p>the some more text down here</p>
<h1>Another bunch of stuff</h1>
<p>then some other junk</p>
うまくいけば、必要に応じて他のタグを作成する余地を残すことができます
編集:私の質問は、正規表現関数を記述して、特定のテキストを変換し、開始と終了のすべてのインスタンスを見つけて ** 、適切なまたはタグに置き換える方法です。私は持っています:再インポート
header_pattern = re.compile(r'(?P**)(?P.*)(?P**)', re.MULTILINE)
def format_headers(text):
def process_match(m):
return "<h2>%s</h2>" % m.group('header')
new_text = header_pattern.sub(process_match, text)
print new_text
ただし、これは最初の ** と最後の ** のみを取得し、途中のものは無視します。