# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^#(.*)((?:.|[\n])+?)(?=^#|\Z)"
test_str = ("\n"
"# 1、孔子语录(论语)\n"
"子曰:“学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?”\n"
"子曰:“温故而知新,可以为师矣。”\n"
"子曰:“学而不思则罔,思而不学则殆。”\n"
"子曰:“由,诲女知之乎?知之为知之,不知为不知,是知也。”\n"
"子贡问曰:“孔文子何以谓之‘文’也?”子曰:“敏而好学,不耻下问,是以谓之‘文’也。”\n"
"子曰:“默而识之,学而不厌,诲人不倦,何有于我哉?”\n"
"子曰:“三人行,必有我师焉。择其善者而从之,其不善者而改之。”\n"
"子曰:“知之者不如好之者,好之者不如乐之者。”\n"
"子在川上曰:“逝者如斯夫,不舍昼夜。”\n"
"子曰:“吾尝终日不食,终夜不寝,以思,无益,不如学也。”\n\n"
"# 2、鱼我所欲也(《孟子·告子上》)\n"
" 鱼,我所欲也,熊掌,亦我所欲也,二者不可得兼,舍鱼而取熊掌者也。生,亦我所欲也,义,亦我所欲也,二者不可得兼,舍生而取义者也。生亦我所欲,所欲有甚于生者,故不为苟得也。死亦我所恶,所恶有甚于死者,故患有所不辟也。如使人之所欲莫甚于生,则凡可以得生者何不用也。使人之所恶莫甚于死者,则凡可以辟患者何不为也!由是则生而有不用也,由是则可以辟患而有不为也。是故所欲有甚于生者,所恶有甚于死者。非独贤者有是心也,人皆有之,贤者能勿丧耳。\n"
" 一箪食,一豆羹,得之则生,弗得则死。呼尔而与之,行道之人弗受;蹴尔而与之,乞人不屑也。\n"
" 万钟则不辩礼义而受之,万钟于我何加焉!为宫室之美,妻妾之奉,所识穷乏者得我与?乡为身死而不受,今为宫室之美为之;乡为身死而不受,今为妻妾之奉为之;乡为身死而不受,今为所识穷乏者得我而为之;是亦不可以已乎?此之谓失其本心。\n\n"
"# 3、生于忧患,死于安乐(《孟子·告子下》)\n"
" 舜发于畎亩之中,傅说举于版筑之间,胶鬲举于鱼盐之中,管夷吾举于士,孙叔敖举于海,百里奚举于市。\n"
" 故天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,曾益其所不能。\n"
" 人恒过,然后能改;困于心,衡于虑,而后作;征于色,发于声,而后喻。入则无法家拂士,出则无敌国外患者,国恒亡。然后知生于忧患而死于安乐也。")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html