Please bear with me, I'm not well versed with Pattern
, Matcher
or Regex
.
I found the below on https://regex101.com/
- South African passport:
[A|D|M|T][0-9]{8}
- South African Id:
(([0-9]{2})(0|1)([0-9])([0-3])([0-9]))([ ]?)(([0-9]{4})([ ]?)([0-1][8]([ ]?)[0-9]))
- Salesforce CS:
(?i)^c\\s\\-\\s\\d{8}
I combined them into: [A|D|M|T][0-9]{8}|(([0-9]{2})(0|1)([0-9])([0-3])([0-9]))([ ]?)(([0-9]{4})([ ]?)([0-1][8]([ ]?)[0-9]))|(?i)^c\\s\\-\\s\\d{8}
and it works on the regex101 website but it doesn't work in Apex.
I know regex works differently in Apex but I'm not exactly sure what to change.
- Sample South African Id:
9202204720082
- Sample South African Passport:
A01524783
- Case Number:
C - 12246503
Here's my method in Apex:
public String findMatch(
String text,
String regex
) {
Matcher emailMatcher = Pattern.compile(regex).matcher(text);
return (emailMatcher.find()) ? emailMatcher.group() : '';
}
What do I need to change to make it work? Any help is appreciated.