PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/ svn-cassiopeia --username yuuma.tomita@gmail.com/cas_helper/xldb/utils/re_tools/re_test.py

http://svn-cassiopeia.googlecode.com/
Python | 80 lines | 79 code | 1 blank | 0 comment | 0 complexity | 8e78d530865cf6dd7716bfe1fd7e5ab6 MD5 | raw file
  1. import re
  2. def repl_f():
  3. def rep(mo):
  4. return ''.join(mo.groups())
  5. return rep
  6. t='abcd'
  7. _re = re.compile(r'a(bc)d')
  8. _re.search(t).groups()
  9. _re.sub(r'\g<1>', t)
  10. _re.sub(repl_f(), t)
  11. import re, dateutil
  12. from xldb.sheetutils import get_named_tablib
  13. from xldb.utils.parsers.date_parser import date_normalize
  14. from xldb.utils.parsers.mapp import mapp
  15. re_flag = re.I|re.U|re.MULTILINE|re.DOTALL|re.VERBOSE
  16. d = get_named_tablib('jwits', 'odekake', u'???')
  17. whole = '\n'.join(d[u'??'])
  18. def replacer_factory(repl_exp=None): #m=match_object
  19. def replacer(mo):
  20. res = None
  21. if mo:
  22. items = [item.replace('\r', '').replace('\n', '') for item in mo.groups() if item ]
  23. res = ' '.join(items)
  24. return res
  25. return replacer
  26. def replacer(mo):
  27. res = None
  28. if mo:
  29. items = [item for item in mo.groups() if item ]
  30. res = ' '.join(items)
  31. return 'replacer'
  32. main_exp = ur"""
  33. .+??????(?????????????????|[\s]*)[\s]*(?P<M>.+?)[\s]+[=]{4,}.+
  34. """; main_re = re.compile(main_exp, re_flag)
  35. print u'<{0}>'.format(main_re.sub(r'\g<M>', whole)[0:100])
  36. #.+??????(?????????????????|[\s]*)[\s]*(?P<M>.+?)[\s]+??????.+
  37. tt = u"""
  38. ???????????????
  39. ???????
  40. ???????????????????????
  41. ??????????????5.5?H? ?????????6.0?H?
  42. ???????23?3?17?????AM??PM14?00
  43. -----------------------------------------------------------------------------------
  44. """
  45. main_exp = ur"""
  46. .+^????[^?]+?[^\d]+([\d.]+).+
  47. """; main_re = re.compile(main_exp, re_flag)
  48. print u'<{0}>'.format(main_re.sub(r'\g<1>', tt)[0:100])
  49. #.+^????[?\s]+[^?]+?([^\n?]+?)[\s]*[\n].+
  50. print main_re.match(whole).groups()[0]
  51. print main_re.sub(replacer, tt)
  52. print main_re.sub(replacer, whole)
  53. print main_re.sub(r'\g<1>', whole)[0:300]
  54. main_rep = None
  55. main_value = main_re.sub(replacer_factory(main_rep), whole)
  56. main_re.sub(r'abc', whole)