# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(.*?)\.((?:\.?[0-9]+){3,}(?:[-a-z]+)?)\.nupkg$"
test_str = ("knockoutjs.3.4.2.nupkg\n"
"log4net.2.0.8.nupkg\n"
"runtime.tizen.4.0.0-armel.microsoft.netcore.jit.2.0.0.nupkg\n"
"nuget.core.2.7.0-alpha.nupkg\n"
"microsoft.identitymodel.6.1.7600.16394.nupkg\n\n"
"Package: knockoutjs Version: 3.4.2\n"
"Package: log4net Version: 2.0.8\n"
"Package: runtime.tizen.4.0.0-armel.microsoft.netcore.jit Version: 2.0.0\n"
"Package: nuget.core Version: 2.7.0-alpha\n"
"Package: microsoft.identitymodel Version: 6.1.7600.16394\n")
subst = "Package: \\1 Version: \\2"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
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.
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