we can use the regular expression and utilize the Apex Pattern and Matcher classes.
Below is the generic regular expression to get the specific tags information from the HTML string.
Replace your tag and use it for your requirement.
(?i)<test([^>]+)>(.+?)</test>Ex:
(?i)<div([^>]+)>(.+?)</div>
from the below string, I am extracting the a (anchor) tag information.
Sample Html String:
"We at <a href="https://www.linkedin.com/company/etg/" rel="noopener noreferrer" target="_blank">ETS, Inc.</a> are eager to share with you that we are a Strategic Solution Partner for the <a href="https://www.linkedin.com/company/commercecloud-demandware/" rel="noopener noreferrer" target="_blank">Salesforce Commerce Cloud</a>.<br>If you need any assistance with your <a href="https://www.linkedin.com/feed/hashtag/?keywords=ecommerce&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6721841224095014912" rel="noopener noreferrer" target="_blank">#eCommerce</a> systems, let us know how we can help!"
From the above string, I am getting the anchor tag information.
Sample Code:
String strMessage = 'We at <a href="https://www.linkedin.com/company/etg/" rel="noopener noreferrer" target="_blank">ETS, Inc.</a> are eager to share with you that we are a Strategic Solution Partner for the <a href="https://www.linkedin.com/company/commercecloud-demandware/" rel="noopener noreferrer" target="_blank">Salesforce Commerce Cloud</a>.<br>If you need any assistance with your <a href="https://www.linkedin.com/feed/hashtag/?keywords=ecommerce&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6721841224095014912" rel="noopener noreferrer" target="_blank">#eCommerce</a> systems, let us know how we can help!';
Pattern objPattren = Pattern.compile('(?i)<a([^>]+)>(.+?)</a>');
Matcher objMatcher = objPattren.matcher(strMessage);
while (objMatcher.find()) {
String groupValue = objMatcher.group();
System.debug('groupValue => '+groupValue);
}
Output:
No comments:
Post a Comment