1

I have created a custom field of type Text Area in my Sandbox environment and would like to know how to normalize the text typed into this custom field.

In order to make a normalization I've implememted the following method:

public String domainNormalization(Account accountObj) {
    String domain = accountObj.Domain__c;
    if (domain != null) {
        domain = domain.replaceAll('[ ]+', '');
        domain = domain.replaceAll('[,;:*]+', ' ');
        domain = domain.replaceAll('(https?://www.|https?|www.)', ' ');
        domain = domain.replaceAll('(A-Za-z\\d._~:/?#\\[\\]@!$&\'()*+,;=-)', ' ');
        accountObj.Domain__c = domain;
        return domain;
    }
    return '';
}

But when I am able to add in the domain field: Screenshot 1

The code in the normalization method does not work because I get the following: Screennshot 2

It should always look for string(domainname) + dot "." + the extension of the domain(com, eu, bg and etc). Where the separator between these must be a single space for example: domain.com secondomain.com thirddomain.com

Coud you please advise how to normalize the text typed in the domain field in order to achive the same result in the given example?

I also would like to know is there a way to get all possible domain extensions(com, eu, bg and etc)? Do I need special consideration about domains such as for instance (co.uk)?

  • Don't you have to escape the * character? – Adrian Larson Feb 4 '16 at 16:45
  • I have used the following regex ('(A-Za-z\\d._~:/?#\[\]@!$&\'()*+,;=-)', ' '); which indeed escape the asterisk character but still can't normalize the text typed in the domain field and get the result I want. Here is the what I get after the asterisk charatcter is escaped: – Dylan D Feb 5 '16 at 13:30
  • ha ha.hatest omg testdomain.comseconddomain.comthriddomain.com – Dylan D Feb 5 '16 at 13:33
  • The result I want to get is the following: – Dylan D Feb 5 '16 at 13:47
  • testdomain.comseconddomain.comthriddomain.com – Dylan D Feb 5 '16 at 14:49
1

I don't know exactly what you expect this string to contain, but it seems to me that you may be making this much more difficult than it needs to be. I would suggest you try doing something like this:

public String domainNormalization(Account accountObj) {
    String domain = accountObj.Domain__c;
    if (domain != null) {
        domain = domain.normalizeSpace();
        // removes all leading, trailing and repeating whitespace

        // If there's no '.' in the string that follows the domain
        domain = domain.substringAfterLast(.);

        //* ***If the the domain doesn't follow the last period***

        domain = domain.remove('http|https|:|\/\/|www.');

        domain = domain.remove(',|;');
        // no need to insert a space, just remove

        // in doing the below it seems you'd be removing content from wrong places
        // domain = domain.replaceAll('(A-Za-z\\d._~:/?#\\[\\]@!$&\'()*+,;=-)', ' ');
        // it seems the thing to do now is to use split and count the number of returned 
        list<string>domainparts = domain.split('.');

        if(domainparts.size()>2){
        // you need to figure out which one of the strings has what you're looking for and remove the '.' from the end of it

        else if(domainparts.size()==1){
           domain = domainparts[1];
           //double check to make certain it doesn't end with a period
           if(domainparts[1].endsWith('.')) domain = domainparts[1].removeEnd('.'); 
        }

        ***//

      accountObj.Domain__c = domain;
      return domain;
    }
    return ;
}

And yes, if you have multiple extension like .co.uk, this would be the way to do that. However, you'd need to have a list of them that you'd want to iterate on in order to find them or else create a regex that would find them within the list of splits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.