正则表达式替换特定条件

时间:2016-03-26 03:04:23

标签: c# regex

我有一个这样的字符串:

  

“(Extent2.pending_DLU> CAST('2015-05-26 00:00:00.00000'AS   时间戳))(Extent1.status_DLU> CAST('2015-05-26 00:00:00.00000'   AS时间戳))和(Extent1.RQST_SUBMT_TS = CAST('2015-05-26   00:00:00.00000'AS时间戳)或(Extent2.WOstatus> CAST('2015-05-26   00:00:00.00000'AS时间戳))“

我希望将'timestamp'替换为'date',除非列为RQST_SUBMT_TS

预期产出:

  

“(Extent2.pending_DLU> CAST('2015-05-26 00:00:00.00000'AS   日期))(Extent1.status_DLU> CAST('2015-05-26 00:00:00.00000'AS   date))和(Extent1.RQST_SUBMT_TS = CAST('2015-05-26 00:00:00.00000'   AS时间戳)或(Extent2.WOstatus> CAST('2015-05-26 00:00:00.00000'   AS日期))“

我创建了以下正则表达式,但它不正确:

 /timestamp\b!([A-Za-z0-9()\s]+.RQST_SUBMT_TS\s[<>=]?[<>=]\sCAST\(\'\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{5}\'\sAS\stimestamp)+/g

有人可以帮我找出正确的正则表达式吗?

修改:提供一些背景知识。 我在这里面临同样的问题 Dynamic Expression Casting DateTime as timestamp。 作为一种解决方法,我收到查询并尝试将'timestamp'替换为'date'。但必须仅对具有DATE数据类型的列执行此操作。

3 个答案:

答案 0 :(得分:1)

测试了此代码

 string Data = "(Extent2.pending_DLU > CAST('2015-05-26 00:00:00.00000' AS timestamp))​ (Extent1.status_DLU > CAST('2015-05-26 00:00:00.00000' AS timestamp)) and (Extent1.RQST_SUBMT_TS = CAST('2015-05-26 00:00:00.00000' AS timestamp ) or (Extent2.WOstatus > CAST('2015-05-26 00:00:00.00000' AS timestamp))";
 var pattern = @"timestamp|RQST_SUBMT_TS.*?timestamp";
 var cleanedUpValue = Regex.Replace(Data, pattern, m => m.Value.Length > "timestamp".Length ? m.Value : "date");

<强>输出

(Extent2.pending_DLU > CAST('2015-05-26 00:00:00.00000' AS date))​ (Extent1.status_DLU > CAST('2015-05-26 00:00:00.00000' AS date)) and (Extent1.RQST_SUBMT_TS = CAST('2015-05-26 00:00:00.00000' AS timestamp ) or (Extent2.WOstatus > CAST('2015-05-26 00:00:00.00000' AS date))

答案 1 :(得分:0)

尝试以下

/\(([A-Za-z0-9()\s]+.RQST_SUBMT_TS\s[<>=]?[<>=]\sCAST\(\'\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{5}\'\sAS\stimestamp)+g

timestamp\b!替换为\(
CAST(\'替换为CAST\(\'

答案 2 :(得分:0)

以下简单正则表达式在您的示例中与timestamp匹配,但我不确定它是否足够灵活:

(?<!RQST_SUBMT_TS.{39})timestamp