Created
March 12, 2016 15:24
-
-
Save utgwkk/c8f0b2e863ee65293821 to your computer and use it in GitHub Desktop.
tr// とか String.tr 的あれ
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
# coding: utf-8 | |
import re | |
def expand_hyphen(pattern_string): | |
def _expand(hyphen): | |
start, end = map(ord, hyphen.groups()) | |
return ''.join(map(chr, range(start, end+1))) | |
return re.sub(r'(\S)\-(\S)', _expand, pattern_string) | |
def tr(s, pattern, replace): | |
return s.translate(str.maketrans(expand_hyphen(pattern), expand_hyphen(replace))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment