I am trying to match DateTime input using the Pattern and Matcher class of Apex but wasn't able to achieve it.
I've created a regex for matching the Datetime input as follows and it matches the datetime input '2021-01-01 12:34' in regex validate site as follows.
However it throws No Match found in Apex,
A simple Utility class as follows,
public with sharing class DateUtility {
public static final Pattern REGEX_DATETIME = Pattern.compile('\\d{2,4}-\\d{1,2}-\\d{1,2}(\\s+)\\d{1,2}');
public static Datetime deserializeDateTime(String bodyDateTimeString) {
Matcher strMatch = REGEX_DATETIME.matcher(bodyDateTimeString);
System.debug('2222 ' + strMatch.matches());
System.debug('ZZZZ ' + strMatch.groupCount());
//Some Logics
Datetime convertedDT;
return convertedDT;
}
}
In Anonymous,
DateTime response = Ppm_DateUtility.deserializeDateTime('2021-01-01 12:34');
System.debug('The response is ' + response);
What I have missed over here?