Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- pattern = r"(# .+?\n)"
- text = r"# Titlen## Chaptern### sub-chapter#### What a lovely day.n"
- header = re.search(pattern, text)
- print(header.string)
- (# .+?\n)(.+)
- # coding=utf8
- # the above tag defines encoding for this document and is for Python 2.x compatibility
- import re
- regex = r"(# .+?\n)(.+)"
- test_str = "# Title\n## Chapter\n### sub-chapter#### The Bar\nIt was a fall day.\n"
- subst = "\1"
- # You can manually specify the number of replacements by changing the 4th argument
- result = re.sub(regex, subst, test_str, 1)
- if result:
- print (result)
- # 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