# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\((?:(?<topro>[^)]*?\bpro\b)(?:(?<u1> +[^)])(?<l1>[^ )]*))?(?:(?<u2> +[^)])(?<l2>[^ )]*))?(?:(?<u3> +[^)])(?<l3>[^ )]*))?(?:(?<u4> +[^)])(?<l4>[^ )]*))? *|(?<withoutpro>.*?))\)"
test_str = ("4377631753733131 (pro alber con tr33)\n"
"886uhghsdfdsfh (pro cios tui tr32)\n"
"rtyqrey 6y46457 6372576 (Flrt ghnap tsr)\n"
"537657QQRFytyvg (flipzz Reyhdf frfg33)\n\n"
"4377631753733131 (pro alber con tr33) (flipzz Reyhdf frfg33) (Foo pro bar pro cios tui tr32)\n"
"537657QQRFytyvg (pro foo ))\n\n"
"I would like all words after \"pro\" to be capitalized. If \"pro\" is not present, all words in the brackets must be lowercase, for have :\n\n"
"4377631753733131 (pro Alber Con Tr33)\n"
"886uhghsdfdsfh (pro Cios Tui Tr32)\n"
"rtyqrey 6y46457 6372576 (flrt ghnap tsr)\n"
"537657QQRFytyvg (flipzz reyhdf frfg33)")
subst = "(${topro}\\U${u1}\\L${l1}\\U${u2}\\L${l2}\\U${u3}\\L${l3}\\U${u4}\\L${l4}\\L${withoutpro})\\E"
# 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