Pattern Matching Strings in Apex (Regex)

regex

This article demonstrates how to use Pattern and Matcher Apex classes in Salesforce.

  1. How do I extract URLs from HTML content in Salesforce?

Sample HTML –

<html>
    <head>
        <title>Test Web Page</title>
    </head>
    <body>
        <a href="https://www.google.com/">Google</a>
        <a href="https://www.yahoo.com/">Yahoo!</a>
        <a href="https://www.facebook.com/">Facebook</a>
    </body>
</html>

Apex Code –

String htmlBody = '<html> <head> <title>Test Web Page</title> </head> <body> <a href="https://www.google.com/">Google</a> <a href="https://www.yahoo.com/">Yahoo!</a> <a href="https://www.facebook.com/">Facebook</a> </body> </html>';

String myRegex = '(?<=href=").*?(?=")';
Pattern myPattern = compile(myRegex);
Matcher myMatcher = myPattern.matcher(htmlBody);

List<String> urls = new List<String>();
while (myMatcher.find()) {
    urls.add(myMatcher.group());
}

System.debug(urls);

Read more –

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_pattern_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_pattern_and_matcher_matcher_methods.htm

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.