function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pavan Kumar.vpavan Kumar.v 

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);
                }
            }
        }

SwethaSwetha (Salesforce Developers) 
HI Pavan,
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/
pavan Kumar.vpavan Kumar.v
Hi Swetha,
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.
SwethaSwetha (Salesforce Developers) 
Got your point.You basically needed to Escape all the \ characters with more \ character

From
'{+61}\d{9}[0-9]'
to​​​
'{+61}\\d{9}[0-9]'
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
pavan Kumar.vpavan Kumar.v
I tried this one too shwetha but still its getting failed :(
Not getting where its getting wrong.
 
SwethaSwetha (Salesforce Developers) 
Is it the same error still?

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
pavan Kumar.vpavan Kumar.v
Hi Swetha,
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
pavan Kumar.vpavan Kumar.v
Hi Swetha its working for this code.
I'm not sure how but I got this from somewhere -  '^[+]61\\d{9}$'