Categories Salesforce
Pattern Matching Strings in Apex (Regex)
This article demonstrates how to use Pattern and Matcher Apex classes in Salesforce.
- 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 –