Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import re
  2.  
  3. pattern = r"(# .+?\n)"
  4.  
  5. text = r"# Titlen## Chaptern### sub-chapter#### What a lovely day.n"
  6.  
  7. header = re.search(pattern, text)
  8. print(header.string)
  9.  
  10. (# .+?\n)(.+)
  11.  
  12. # coding=utf8
  13. # the above tag defines encoding for this document and is for Python 2.x compatibility
  14.  
  15. import re
  16.  
  17. regex = r"(# .+?\n)(.+)"
  18.  
  19. test_str = "# Title\n## Chapter\n### sub-chapter#### The Bar\nIt was a fall day.\n"
  20.  
  21. subst = "\1"
  22.  
  23. # You can manually specify the number of replacements by changing the 4th argument
  24. result = re.sub(regex, subst, test_str, 1)
  25.  
  26. if result:
  27. print (result)
  28.  
  29. # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement