# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\[weight][^\n]*(?:\n(?!$|\n)[^\n]*)*"
test_str = ("#########################\n"
"### MOSES CONFIG FILE ###\n"
"#########################\n\n"
"# input factors \n"
"[input-factors]\n"
"0\n\n"
"# mapping steps\n"
"[mapping]\n"
"0 T 0\n\n"
"[distortion-limit]\n"
"6\n\n"
"# feature functions\n"
"[feature]\n"
"UnknownWordPenalty\n"
"WordPenalty\n"
"PhrasePenalty\n"
"PhraseDictionaryMemory name=TranslationModel0 num-features=4 path=/home /gillin/jojomert/phrase-jojo/work.src-ref/training/model/phrase-table.gz input-factor=0 output-factor=0\n"
"LexicalReordering name=LexicalReordering0 num-features=6 type=wbe-msd-bidirectional-fe-allff input-factor=0 output-factor=0 path=/home/gillin/jojomert/phrase-jojo/work.src-ref/training/model/reordering-table.wbe-msd-bidirectional-fe.gz\n"
"Distortion\n"
"KENLM lazyken=0 name=LM0 factor=0 path=/home/gillin/jojomert/ru.kenlm order=5\n\n"
"# dense weights for feature functions\n"
"[weight]\n"
"UnknownWordPenalty0= 1\n"
"WordPenalty0= -1\n"
"PhrasePenalty0= 0.2\n"
"TranslationModel0= 0.2 0.2 0.2 0.2\n"
"LexicalReordering0= 0.3 0.3 0.3 0.3 0.3 0.3\n"
"Distortion0= 0.3\n"
"LM0= 0.5")
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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