You can get some context here on my previous post.
When I run the below code on execute anonymous, it renders the following result:
String txt = 'Requesting service for Id Number 9202204720082, Case C - 12246503 and passport A01524783';
String regex = '[ADMT]\\d{8}|\\d{2}[01]\\d[0-3]\\d ?\\d{4} ?[01]8 ?\\d|(?i)C - \\d{8}';
System.debug('========' + regex);
Matcher thematcher = Pattern.compile(regex).matcher(txt);
String idno = (thematcher.find()) ? thematcher.group() : '';
System.debug('========= ' + idno);
When I call the class from execute anonymous:
String txt = 'Requesting service for Id Number 9202204720082';
String regex = '[ADMT]\\d{8}|\\d{2}[01]\\d[0-3]\\d ?\\d{4} ?[01]8 ?\\d|(?i)C - \\d{8}';
System.debug('========' + new EmailLinker(new Messaging.InboundEmail()).findMatch(txt, regex));
If I call the class directly from the backend:
public String findMatch(
String emailContent,
String criteriaValue
) {
String idNumber = '';
criteriaValue = criteriaValue.unescapeHtml4();
System.debug('======== Criteria ' + criteriaValue);
System.debug('======== content ' + emailContent);
Matcher emailMatcher = Pattern.compile(criteriaValue).matcher(emailContent);
System.debug('======== email match ' + emailMatcher.find());
Boolean isFound = Boolean.valueOf(emailMatcher.find());
//if (isFound == true) {
System.debug('======== ID ' + emailMatcher.group());
idNumber = emailMatcher.group();
//}
System.debug('======== ID ' + idNumber);
return idNumber;
}
What am I missing here? Any help is appreciated.
Edit: As per the comment
public String findMatch(
String emailContent,
String criteriaValue
) {
String idNumber = '';
criteriaValue = criteriaValue.unescapeHtml4();
System.debug('======== Criteria ' + criteriaValue);// [ADMT]\\d{8}|\\d{2}[01]\\d[0-3]\\d ?\\d{4} ?[01]8 ?\\d|(?i)C - \\d{8}
System.debug('======== content ' + emailContent);
Matcher emailMatcher = Pattern.compile(criteriaValue).matcher(emailContent);
if (emailMatcher.find()) {
System.debug('======== ID ' + emailMatcher.group());
idNumber = emailMatcher.group();
}
System.debug('======== ID ' + idNumber);
return idNumber;
}
This is still not working, it only works when I hardcode the regex pattern, and can't do that as it is dynamic.
System.debug('======== email match ' + emailMatcher.find());
line