正则表达式找到在组a中找不到的字符串或在组b

时间:2018-01-11 19:34:04

标签: sql regex coldfusion

我试图在SQL语句中找到cfml变量(由#anyvariable#标识),这些变量不在cfml <cfqueryparam>标记内,也不在cfml条件块中(由<cfif></cfif>标识) )。我已经写了以下正则表达式:

#anyvariable#可以找到:(#[^##]+#)

<cfqueryparam>内容可以在以下网址找到:(?<=<cfqueryparam)(.*)(?=">)

<cfif></cfif>内容可以在以下网址找到:(?s)(?<=<cfif).*?(?=<\/cfif>)

我坚持的部分是如何将这三个正则表达式结合在一起,最终得到我正在寻找的#anyvariable#results。在psuedo-code中我想要:

FIND (#[^##]+#) 
WHERE (#[^##]+#) NOT IN (?<=<cfqueryparam)(.*)(?=">) 
AND (#[^##]+#) NOT IN (?s)(?<=<cfif).*?(?=<\/cfif>)

这是我正在搜索的示例源代码字符串:

SELECT Clockin, ClockOut, LunchIn, LunchOut, TimeSheetID, Hours, isNull(RegHours,0) as RegHours, isNull(OTHours,0) as OTHours, IsNull(OT2Hours,0) as OT2Hours, IsNull(VacationHours,0) AS VacationHours, IsNull(PersonalHours,0) AS PersonalHours, IsNull(HolidayHours,0) AS HolidayHours, IsNull(SickHours,0) AS SickHours, IsNull(PremiumHours,0) AS PremiumHours, IsNull(OtherHours,0) AS OtherHours FROM TimeSheet WHERE UserID = <cfqueryparam value="#session.am_UserID#"> AND timesheetID = #form.timesheetID# <cfif IsDefined('form.filterby') AND form.filterby IS "PayPeriod"> AND PayPeriodID = #form.PayPeriod# <cfelse> AND clockin between #Createodbcdatetime(Form.StartDate)# and #Createodbcdatetime(dateadd('h',23,Form.EndDate))# </cfif> ORDER BY clockin

生成的正则表达式将找到并返回#form.timesheetID#

的匹配项

1 个答案:

答案 0 :(得分:1)

一个选项可能是通过交替|主动匹配“坏”序列,但不会“记住”(?:),并且您的“好”匹配最后运行并包含在捕获组中{ {1}}:

()

https://regex101.com/r/s3sW7N/3/