code_junkie

python regular expression to split paragraphs

Nov 11th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. the_str = 'paragraph1nnparagraph2'
  2. # splitting should yield ['paragraph1', 'paragraph2']
  3.  
  4. the_str = 'p1ntnp2tntstill p2t n ntp3'
  5. # should yield ['p1', 'p2tntstill p2', 'p3']
  6.  
  7. the_str = 'p1nnntp2'
  8. # should yield ['p1', 'ntp2']
  9.  
  10. from itertools import groupby
  11.  
  12. def paragraph(lines) :
  13. for group_separator, line_iteration in groupby(lines.splitlines(True), key = str.isspace) :
  14. if not group_separator :
  15. yield ''.join(line_iteration)
  16.  
  17. for p in paragraph('p1ntnp2tntstill p2t n ntp'):
  18. print repr(p)
  19.  
  20. 'p1n'
  21. 'p2tntstill p2t n'
  22. 'tp3'
  23.  
  24. s*?ns*?ns*?
Add Comment
Please, Sign In to add comment