# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(<cfldap[^>]* name ?= ?\")(.*?)(\"[^>]*)( sort ?= ?\"(.*?)\")(.*?>)"
test_str = ("<!--- petofma 20090716: new CED LDAP server now wants applications to authenticate! --->\n"
"<CFLDAP server=\"#session.comldapserver#\" port=\"#session.comldapport#\" action=\"QUERY\" name=\"results\" start=\"ou=People, o=#session.comldaproot#\"\n"
" username=\"#server.comLdapLogin#\" password=\"#server.comLdapPass#\"\n"
" scope=\"onelevel\" filter=\"(&#filterstring#(recordStatus=a)#Iif(IsDefined(\"dgfish_only\"),DE(\"(sourceOrganisation=COM)(dg=MARE)\"),DE(\"\"))#)\" sort=\"uid\" attributes=\"dn,employeeNumber,ecSYSPERNumber,gestelNumber,uid,title,cn,dg,departmentNumber,telephoneNumber,facsimilenumber,l,mail,physicalDeliveryOfficeName,sourceOrganisation,employeeType,contractExpiryDate,recordStatus,modifytimestamp,source,manager\">\n"
"<CFOUTPUT>\n"
"<P>Found <b>#results.RecordCount# User#Iif(results.RecordCount EQ 1,DE(\"\"),DE(\"s\"))#</b> matching your search criteria:\n"
"</CFOUTPUT>\n"
"<cfif IsDebugMode()>\n"
" <cfdump var=#results#>\n"
"</cfif>\n")
subst = "$1unsortedResults$3$6\\n<cfquery dbtype=\"query\" name=\"$2\">SELECT * FROM unsortedResults ORDER BY $5</cfquery>\\n"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.IGNORECASE)
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