# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"=\s?.*(?:((?i)Fields\(\"(\w+)\"\)\.Value\)*?))"
test_str = ("Function lmpRoundedEndTime_ValueChanged(sender, e)\n"
"If CDbl(Fields(\"lmpShiftID\").Value) <> 0 And IsNull(Fields(\"lmpRoundedStartTime\").Value) = False And IsNull(Fields(\"lmpRoundedEndTime\").Value) = False Then\n"
" If CInt(App.Props(\"PN\").Fields(\"xapDCPayCalculationMethod\").Value) <> 2 Then\n"
" Fields(\"lmpPayrollHours\").Value = App.Ax(\"TimecardFunctions\").CalculateHours(Fields(\"lmpShiftID\").Value,Fields(\"lmpRoundedStartTime\").Value,Fields(\"lmpRoundedEndTime\").Value)\n"
"'''''''''''' ^ above should capture: lmpShiftID , lmpRoundedStartTime , lmpRoundedEndTime\n"
" End If\n"
"End If\n"
"End Function\n\n"
"Function TimecardLines_SetDefaultValues(sender, e)\n"
" Dim oRs,dTimecardDate\n"
" dTimecardDate = Null\n"
" Set oRs = CreateObject(\"ADODB.Recordset\")\n"
" oRs.Open \"select lmpEmployeeID,lmpShiftID,lmpTimecardDate,lmpProjectID from Timecards where lmpTimecardID = \" + App.Convert.NumberToSql(Fields(\"lmlTimecardID\").Value),Connection,adOpenStatic,adLockReadOnly,adCmdText\n"
" If oRs.EOF = False Then\n"
" Fields(\"lmlEmployeeID\").Value = oRs.Fields(\"lmpEmployeeID\").Value\n"
" Fields(\"lmlShiftID\").Value = oRs.Fields(\"lmpShiftID\").Value\n"
" Fields(\"lmlProjectID\").Value = oRs.Fields(\"lmpProjectID\").Value\n"
" dTimecardDate = oRs.Fields(\"lmpTimecardDate\").Value\n"
" End If\n"
" oRs.Close\n"
" \n"
" oRs.Open \"select top 1 lmlRoundedEndTime From TimecardLines Where lmlTimecardID = \" + App.Convert.NumberToSql(Fields(\"lmlTimecardID\").Value) + \" Order By lmlRoundedEndTime Desc\",Connection,adOpenStatic,adLockReadOnly,adCmdText\n"
" If oRs.EOF = False Then\n"
" Fields(\"lmlRoundedStartTime\").Value = oRs.Fields(\"lmlRoundedEndTime\").Value\n"
" Else\n"
" Fields(\"lmlRoundedStartTime\").Value = App.Ax(\"Shift\").GetDayStartTime(Fields(\"lmlShiftID\").Value, dTimecardDate, e.SqlTransaction)\n"
" ' Added to to fix -rounded end time is earlier than the rounded start time error when changing the labor hours\n"
" 'Fields(\"lmlRoundedEndTime\").Value=Fields(\"lmlRoundedStartTime\").Value \n"
" \n"
" End If\n"
" oRs.Close\n\n"
" If Trim(Fields(\"lmlEmployeeID\").Value) <> \"\" Then\n"
" oRs.Open \"select lmeDirectExpenseID,lmeIndirectExpenseID,lmeDefaultWorkCenterID from Employees where lmeEmployeeID = \" + App.Convert.StringToSql(Fields(\"lmlEmployeeID\").Value),Connection,adOpenStatic,adLockReadOnly,adCmdText\n"
" If oRs.EOF = False Then\n"
" If CInt(Fields(\"lmlTimecardType\").Value) = 1 Then\n"
" If Trim(oRs.Fields(\"lmeDirectExpenseID\").Value) <> \"\" Then\n"
" Fields(\"lmlExpenseID\").Value = oRs.Fields(\"lmeDirectExpenseID\").Value\n"
" End If\n"
" Else\n"
" If Trim(oRs.Fields(\"lmeIndirectExpenseID\").Value) <> \"\" Then\n"
" Fields(\"lmlExpenseID\").Value = oRs.Fields(\"lmeIndirectExpenseID\").Value\n"
" End If\n"
" End If\n"
" If CInt(Fields(\"lmlTimecardType\").Value) = 2 Then\n"
" If Trim(oRs.FIelds(\"lmeDefaultWorkCenterID\").Value) <> \"\" Then\n"
" Fields(\"lmlWorkCenterID\").Value = oRs.FIelds(\"lmeDefaultWorkCenterID\").Value\n"
" End If\n"
" End If\n"
" End If\n"
" oRs.Close\n"
" End If\n"
"End Function")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.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