Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Last active December 13, 2015 17:59
Show Gist options
  • Select an option

  • Save nobonobo/4952294 to your computer and use it in GitHub Desktop.

Select an option

Save nobonobo/4952294 to your computer and use it in GitHub Desktop.
自作エスケープ処理。想定外のパターンにちゃんとエラーを上げる。
# encoding: utf-8
import re
pattern = re.compile(r"""
{0}(?:
(?P<escaped>{0}) | # Escape sequence of two delimiters
(?P<tab>t) | # Escape sequence of tab-code
(?P<cr>r) | # Escape sequence of cr-code
(?P<lf>n) | # Escape sequence of lf-code
(?P<invalid>[^{0}tnr]) # Other ill-formed delimiter exprs
)
""".format(re.escape('\\')), re.IGNORECASE | re.VERBOSE)
def func(mo):
m = mo.groupdict()
if m['invalid'] is not None:
raise SyntaxError('Unknown escape: \\{0}'.format(m['invalid']))
elif m['tab'] is not None:
return '\t'
elif m['cr'] is not None:
return '\r'
elif m['lf'] is not None:
return '\n'
return '\\'
decoded = pattern.sub(func, r'\\\t\n\r')
print decoded.encode('hex')
@nobonobo
Copy link
Author

末尾にデリミタ来た場合マッチせずスルーしちゃう。そこもエラーで拾いたい場合はもうひと工夫いるかも。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment