AruRen
AruRen

Reputation: 3

Regex replace for specific condition

I have a string like this:

"(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))"

I want to replace 'timestamp' with 'date' except when the column is RQST_SUBMT_TS.

Expected output:

"(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))"

I created the following regex but it is not correct:

 /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

Could anyone please help me figure out the correct regex pattern to do this?

Edit : To give some background. I am facing the same issue mentined here Dynamic Expression Casting DateTime as timestamp . As a workaround i am getting the query and trying to replace 'timestamp' with 'date' . but it has to be done only for columns with DATE datatypes.

Upvotes: 0

Views: 74

Answers (3)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 19007

Tested this code

 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");

Output

(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))

Upvotes: 1

lekso
lekso

Reputation: 1813

The following simple regex matches timestamp in your example, but I'm not sure if it is flexible enough for you:

(?<!RQST_SUBMT_TS.{39})timestamp

Upvotes: 0

Eminem
Eminem

Reputation: 7484

Try the following

/\(([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

Replaced timestamp\b! with \(
Replaced CAST(\' with CAST\(\'

Upvotes: 0

Related Questions