サンプルコード
regexp.py
# -*- coding: utf-8 -*- import re class YmdRegexp: def ymd_matcher(self, value): return True if self._get_ymd_pattern().match(value) else False def ymd_replace(self, value, after): return self._get_ymd_pattern().sub(value, after) def _get_ymd_pattern(self): return re.compile('^(\d){4}\/(\d){2}\/(\d){2}$') year_regexp = YmdRegexp() print year_regexp.ymd_matcher("2012/04/02") print year_regexp.ymd_matcher("2012/0402") print year_regexp.ymd_matcher("2012/04/2") print year_regexp.ymd_replace("2012/04/02", "hoge")
出力
$python regexp.py
True
False
False
hoge