Regex not working

Sample code

I have made a sample code:

import java.time.LocalDateTime
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.regex.Matcher
import java.util.regex.Pattern

import com.kms.katalon.core.util.KeywordUtil

String datetimeString = "Apr. 28, 2021 16:29:55 GMT"

// parse the string with Regex
Pattern p = Pattern.compile("([A-Za-z]{3})\\. ([0-9]{1,2}), ([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) ([A-Z]{3})")
Matcher m = p.matcher(datetimeString)
if (m.find()) {
	println "Month=${m.group(1)} Day=${m.group(2)} Year=${m.group(3)} Hours=${m.group(4)} Minutes=${m.group(5)} Seconds=${m.group(6)} Zone=${m.group(7)}"
} else {
	throw new IllegalStateException("'${datetimeString}' does not match regex '${p.toString()}'")
}

// converting String to ZonedDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM. dd, yyyy HH:mm:ss z").withLocale(Locale.ENGLISH)
ZonedDateTime zonedDateTime = ZonedDateTime.parse(datetimeString, formatter)
println zonedDateTime

// verify if the given datetime is in between [now: 1week ago]
LocalDateTime now = LocalDateTime.now()
LocalDateTime oneWeekAgo = now.minusDays(7)
LocalDateTime givenDT = zonedDateTime.toLocalDateTime()
if (now.compareTo(givenDT) >= 0 && givenDT.compareTo(oneWeekAgo) > 0) {
	println "the given datetime ${givenDT} is in between now (${now}) and 1 week ago (${oneWeekAgo})"
} else {
	KeywordUtil.markFailedAndStop("the given datetime ${givenDT} is NOT in between now (${now}) and 1 week ago (${oneWeekAgo})")
}

When the above Test Case is executed, the following message comes up in the console

...
Month=Apr Day=28 Year=2021 Hours=16 Minutes=29 Seconds=55 Zone=GMT
...
the given datetime 2021-04-28T16:29:55 is in between now (2021-04-29T13:46:01.542) and 1 week ago (2021-04-22T13:46:01.542)

Description

Your regular expression

  • [A-Za-z]{3} [0-9]{1,2}, [0-9]{4} [0-5]{0,1}[0-9]:[0-5]{0,1}[0-9]:[0-5]{0,1}[0-9] (AM |PM )?((?!UTC)[A-Za-z]{1,6}|UTC((-|+)[0-9]{2})?)

has several problems.

  1. The given string starts with “Apr.”, which has a dot(.). Your regex ignores the dot. Therefore your regex fails to match.
  2. Your regex is unnecessary complex; it has redundant parts (“AM”, “PM”, “UTC”). A simpler regex would suffice to parse the given string into string components.

If you want to perform any date-time calculation, it is not enough to parse a given string into string components with regex.

You should parse a given string with java.time.DateTimeFormetter to convert to java.time.LocalDateTime or java.time.ZoneDateTime object. The classes in the java.time package enable you to perform various calculations over date-time data out of box. See the above snippet as example.

There are a few useful tutorials on the “java.time” package:

1 Like