You need to sign in to do that
Don't have an account?
Regex Apex Code for Australia and New Zealand phone number
Hi All,
Can anyone help in writing the regex code for apex and not for validation rule.
My output should be example: +61918273645 0r +64918273645.
list<string>updatedPhoneNumbers = new list<string>();
String phoneRegex = '{+61}\d{9}[0-9]'; //Australia
String phoneRege = '{+64}\d{9}[0-9]'; // New Zealand
String phoneNumber = '';
list<string>updatedPhoneNu = participantPhone;
for(string str : updatedPhoneNu){
phoneNumber= str ;
Pattern PhonePattern = Pattern.compile(phoneRegex);
Matcher EmailMatcher = PhonePattern.matcher(phoneNumber);
if(EmailMatcher.matches()){
updatedPhoneNumbers.add(str);
}
else {
Pattern PhonePattern1 = Pattern.compile(phoneRegex);
Matcher EmailMatcher1 = PhonePattern.matcher(phoneNumber);
if(EmailMatcher.matches())
{
updatedPhoneNumbers.add(str);
}
}
}
I see you already have apex code in place.Where are you stuck?
Similar posts:
https://salesforce.stackexchange.com/questions/283176/apex-phone-validation
https://sfdcfanboy.com/2017/08/02/a-tip-a-day-7-us-phone-validation-using-regex/
Thanks for replying.
Im getting wrong at Regex Syntax.
Im not getting how to write the regex code for apex for the Output +61918273645 0r +64918273645.
From to Similar post: https://salesforce.stackexchange.com/questions/235931/invalid-string-literal-s-d-s-illegal-character-sequence-s-in-stri
If this information helps, please mark the answer as best. Thank you
Not getting where its getting wrong.
Line: 2, Column: 29
Illegal string literal: Invalid string literal '{+61}\d{9}[0-9]'. Illegal character sequence \d' in string literal.
I tried the code snippet in my dev console and the error vanished after making said changes. Can you share reproducible code snippet that I can try on my org to be able to suggest better?Thanks
sorry for the late reply.
I'm getting error as Invalid regex: Illegal repetition
if I use the above formula.
this is the present code I'm using '{+64}\\d{9}[0-9]'
I need to get output as +64876789435
I'm not sure how but I got this from somewhere - '^[+]61\\d{9}$'