Last active
December 13, 2015 17:59
-
-
Save nobonobo/4952294 to your computer and use it in GitHub Desktop.
自作エスケープ処理。想定外のパターンにちゃんとエラーを上げる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
末尾にデリミタ来た場合マッチせずスルーしちゃう。そこもエラーで拾いたい場合はもうひと工夫いるかも。