You need to sign in to do that
Don't have an account?
Need RegEx to replace 1234567890 with (123) 456-7890
I need to replace a 10 digit string - a phone number with a format like (123) 456-7890 because unforutnately, the DB a total mess.
I can do it in PHP like this:
I can do it in PHP like this:
$data = '1234567890';
if( preg_match( '/^\d(\d{3})(\d{3})(\d{4})$/', $data, $matches ) )
{
$result = '('.$matches[1] . ') ' .$matches[2] . '-' . $matches[3];
return $result;
}
if( preg_match( '/^\d(\d{3})(\d{3})(\d{4})$/', $data, $matches ) )
{
$result = '('.$matches[1] . ') ' .$matches[2] . '-' . $matches[3];
return $result;
}
Which works fine in PHP, but't I can't seem to translate it to APEX. Any help would be apprecieated.
so store your value from the field and strip:
string x = contact.phone.replaceall('//D','');
//probably want to verify you still have 10 digits...and if not do cleanup if greating (leading 1's, etc)
//then do substrings
contact.phone = '(' + x.substring(0,2) + ') ' + x.substring(2,5) + '-' + x.substring(5,9);
All Answers
so store your value from the field and strip:
string x = contact.phone.replaceall('//D','');
//probably want to verify you still have 10 digits...and if not do cleanup if greating (leading 1's, etc)
//then do substrings
contact.phone = '(' + x.substring(0,2) + ') ' + x.substring(2,5) + '-' + x.substring(5,9);
I think you try this link : for your requirement.
http://www.oyecode.com/2011/07/regular-expressions-validate-phone.html