Skip to content

Instantly share code, notes, and snippets.

@utgwkk
Created March 12, 2016 15:24
Show Gist options
  • Save utgwkk/c8f0b2e863ee65293821 to your computer and use it in GitHub Desktop.
Save utgwkk/c8f0b2e863ee65293821 to your computer and use it in GitHub Desktop.
tr// とか String.tr 的あれ
# 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