fork download
  1. haystack1 = '''**GS***RL*CITGO*JJ*20090518*1036*25110*X*00405
  2. ST*423*2511*RT**GS***08247'''
  3. haystack2 = '''**#**UPT 0000SWSAM001203081454NS /
  4. GS*QM*TRSC*UPDS *20120309*0820*0309***#***004010'''
  5.  
  6. fail3 = 'G#' # should only match the '#'
  7.  
  8. import re
  9. needle = re.compile(r'GS|#')
  10.  
  11. def test(**kwargs):
  12. for k, v in kwargs.items():
  13. m = needle.search(v)
  14. if m:
  15. print '{name}: {chars} characters at {start}..{end}'.format(name=k, start=m.start(), end=m.end(), chars=len(m.group()))
  16. else:
  17. print '{name}: no match'.format(name=k)
  18.  
  19. test(haystack1=haystack1, haystack2=haystack2, fail3=fail3)
Success #stdin #stdout 0.01s 7728KB
stdin
Standard input is empty
stdout
fail3: 1 characters at 1..2
haystack2: 1 characters at 2..3
haystack1: 2 characters at 2..4