次のような文字列があります。
### init.sh [bash]
#!/c/Progra~2/Git/bin/sh.exe
git commit -am "[$1] $2"; git push
「init.sh」と「[bash]」の両方を一致させようとしましたが、 RegExr では正常に機能する正規表現文字列がありますが、 Python では機能しません。
正規表現文字列は^(?<!\\)###\s*(.*?)(?:\[(.+?)\])?\s*$(?m)
で、次の方法で一致を取得しています
for match in _section_marker_re.finditer(code):
filename, lang = match.groups()
どこ_section_marker_re
にありre.compiled(regex)
、code
正規表現したい文字列です。
ここでPythonで何が起こっている可能性がありますか?
編集:私はPython 2.7.3を使用していますが、VERBOSEはオンになっていません。
編集 2 : この文字列を使用するコードは次のようになります。
def highlight_multifile(code):
"""Multi-file highlighting."""
result = []
last = [0, None, 'text']
def highlight_section(pos):
start, filename, lang = last
section_code = _escaped_marker.sub('', code[start:pos])
if section_code:
result.append(u'<div class="section">%s%s</div>' % (
filename and u'<p class="filename">%s</p>'
% escape(filename + ' - ' + lang) or u'',
highlight(section_code, lang)
))
for match in _section_marker_re.finditer(code):
start = match.start()
highlight_section(start)
filename, lang = match.groups()
if lang is None:
lang = get_language_for(filename)
else:
lang = lookup_language_alias(lang)
last = [match.end(), filename, lang]
highlight_section(len(code))
return u'<div class="multi">%s</div>' % u'\n'.join(result)
re.compile(r'^\\(?=###)(?m)')
次のセクションの開始を示す_escaped_marker を として使用しています。