Salesforce Developer Regex

salesforce-developer

Pattern p = Pattern.compile('/Accounts/(.+)/contacts');
Matcher matcher = p.matcher(request.requestURI + '');
matcher.find();
System.debug('Match count:' + matcher.groupCount());
String accountId = matcher.group(1);

System.debug('Account ID from test:' + accountId);

String str;
str.replaceAll('[^\w]','');
List<String> templateIds = new List<String>();
List<Campaign> campaigns = [select EmailTemplate__c from Campaign where IsActive=true AND EmailTemplate__c <> null];
if (campaigns.size() >= 0) {
    Integer size = campaigns.size();
    for (Integer i = 0; i < size; i++) {
        Campaign campaign = campaigns.get(i);
        templateIds.add(campaign.EmailTemplate__c);
    }
}

List<EmailTemplate> templatesAll = new List<EmailTemplate>();
for (List<EmailTemplate> templates : [SELECT id,HtmlValue,Body FROM EmailTemplate WHERE Id IN :templateIds]) {
    Integer size = templates.size();
    System.debug('Size:' + size);
    for (Integer i = 0; i < size; i++) {
        EmailTemplate template = templates.get(i);
        System.debug('i:' + i);
        System.debug(template.HtmlValue);
        templatesAll.add(template);
    }
}

Pattern startPattern = Pattern.compile('(?mi)^\\s*<html>\\s*<head>\\s*<title>\\s*</title>\\s*</head>\\s*<body>');
Pattern endPattern = Pattern.compile('(?mi)\\s*</body>\\s*</html>\\s*$');

List<EmailTemplate> modifiedTemplates = new List<EmailTemplate>();
Integer size = templatesAll.size();
for (Integer i = 0; i < size; i++) {
    EmailTemplate template = templatesAll.get(i);
    String html = template.HtmlValue;
    Matcher startMatcher = startPattern.matcher(html);
    if (startMatcher.find()) {
        html = startMatcher.replaceFirst('');
        Matcher endMatcher = endPattern.matcher(html);
        html = endMatcher.replaceFirst('');
        System.debug('HTML:' + html);
        template.HtmlValue = html;
        modifiedTemplates.add(template);
    }
}

The above code demonstrate how we can use regular express flags such as (?mi). The letter i mean case-insensitive, and the letter m mean multiline.

Regular Express in Apex, for the most part, is similar to regular expression in Java

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License