Home > Software design >  Regex to check if based on one condition other condition is met
Regex to check if based on one condition other condition is met

Time:03-23

I am trying to check along with other requirements if to_date is present then it must have current_date and if to_timestamp is present in string then enforce condition that current_date_hour must be present in string.

var filterExpr = "to_date(column, format) = 'current_date-3' "
var pattern = "(?i)(to_date|to_timestamp)\\(.*\\)\\s*={1,2}\\s*'current_date(_hour)?(\\s*-\\s*\\d )'".toRegex()
if (pattern.containsMatchIn(filterExpr!!)) {
    println("it's there")
} else {
    println("not found")
}

At present the above regex also works for mismatched cases.

Any help is appreciated

CodePudding user response:

Add a look ahead for the conditions:

(?=to_date.*current_date\\b|to_timestamp.*current_date_hour)

Adding this to your existing pattern:

var pattern = "(?i)(?=to_date.*current_date\\b|to_timestamp.*current_date_hour)(to_date|to_timestamp)\\(.*\\)\\s*={1,2}\\s*'current_date(_hour)?(\\s*-\\s*\\d )'".toRegex()
  • Related