# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(public (?:(?!{|event|delegate)[^=;])*);"
test_str = ("Regex: ^public ((?!({))(?!=).)*;\n\n\n"
" //MATCHES CORRECTLY\n"
"public string Country;\n"
"public string EmailAddress;\n"
"public string FirstName;\n"
"public string LastName;\n"
"public string PhoneNumber;\n"
"public string PostalCode;\n"
"public string State;\n"
"public List<Object> Example;\n\n"
" //MATCHES BUT SHOULDNT\n"
"public event GetUserListCompletedEventHandler GetUserListCompleted;\n"
"public delegate void GetUserListCompletedEventHandler(object sender, GetUserListCompletedEventArgs e);\n\n"
" //DOES NOT MATCH CORRECTLY\n"
"throw new Exception(\"In order to get the public date for blah blah blah\");\n"
"public List<Order> orderDetails = new List<Order>(); \n"
"public string CustomerName { get; set; }\n"
"public string BillingAddress1 { get; set; }\n"
"public string BillingAddress2 { get; set; }")
subst = "$1 { get; set; }"
# 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