fork download
  1. import re
  2.  
  3. text = """System Id Interface Circuit Id State HoldTime Type PRI
  4. --------------------------------------------------------------------------------
  5. rtr1.lab01.some GE0/0/1 0000000001 Up 22s L2 --
  6. thing
  7. rtr2.lab01.some GE0/0/2 0000000002 Up 24s L2 --
  8. thingelse
  9. rtr2.lab01.abcd GE0/0/4 0000000003 Up 24s L2 --
  10. rtr2.lab01.none GE0/0/24 0000000004 Up 24s L2 --
  11. sense
  12. rtr2.lab01.efgh GE0/0/5 0000000003 Up 24s L2 --
  13. """
  14.  
  15. lines = text.rstrip().split('\n')[2:]
  16. n_lines = len(lines)
  17. current_line = -1
  18.  
  19. def get_next_line():
  20. # Actual implementation would be reading from a file and yielding lines one line at a time
  21. global n_lines, current_line, lines
  22. current_line += 1
  23. # return special sentinel if "end of file"
  24. return lines[current_line] if current_line < n_lines else '$'
  25.  
  26.  
  27. def get_system_id():
  28. line = None
  29. while line != '$': # loop until end of file
  30. if line is None: # no current line
  31. line = get_next_line()
  32. if line == '$': # end of file
  33. return
  34. m = re.search(r'^([a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9])', line)
  35. id = m[1]
  36. line = get_next_line() # might be sentinel
  37. if line != '$' and re.match(r'^[a-zA-Z0-9]+$', line): # next line just single id?
  38. id += line
  39. line = None # will need new line
  40. yield id
  41.  
  42. for id in get_system_id():
  43. print(id)
  44.  
Success #stdin #stdout 0.02s 9388KB
stdin
Standard input is empty
stdout
rtr1.lab01.something
rtr2.lab01.somethingelse
rtr2.lab01.abcd
rtr2.lab01.nonesense
rtr2.lab01.efgh