I have a HTML data in string format and i need to split the HTML string by using Regex pattern matcher and get the tables into list of strings.
<html>
<head>
<script type="text/javascript" language="javascript" src="../javascript/master.js"></script>
<script type="text/javascript" language="javascript" src="../javascript/fusioncharts/FusionCharts.js"></script>
</head>
<title>Copy of Daily Flow Summary</title>
<body>
<div id="reportDetail">
<div id="reportHeader">
<h1>Copy of Daily Flow Summary</h1>
<span style="padding-left: 3px;"></span>
</div>
<h3 >
<br />
<b><span>Report Period:</span></b>
02/14/2018 - 02/14/2018
</h3>
<h3 >
<br />
<b><span>Run Date:</span></b>
02/14/2018
</h3>
<br/>
<div id="reportGraph" style="z-index: 0;width:100%"></div>
<div id="reportData">
<div xmlns:pd4ml="http://pd4ml.com" class="pdfHidden"><img src="/pw/images/s.gif" height="50px">
<table class="statementHeaderTable">
<tbody>
<tr>
<td>A</td>
<td>B</td>
</tr>
</tbody>
</table>
<table class="statementHeaderTable">
<tbody>
<tr>
<td>C</td>
<td>D</td>
</tr>
</tbody>
</table>
</body>
</html>
I tried using the below code and when i hardcode the above html content in the responseBody string, i am able to get the tables correctly but when i try to use the same code with the html content retrieved via url, it doesn't find any match.
String responseBody = response.getBody();
String TableExpression = '<table[^>]*>(.*?)</table>';
Pattern myPattern = Pattern.compile(TableExpression);
Matcher myMatcher = myPattern.matcher(responseBody);
while(myMatcher.find()){
System.debug('my Matcher : ' + myMatcher.group());
}
I think the issue is due to the html string returned via url has multilines because when i remove the "RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase" values from the below c# code, it doesn't give any result. Can anyone suggest me an alternative to these options in apex?
I am able to get the same result in c# using the below code.
string html = "html string retrieved via url";
string TableExpression = "<table[^>]*>(.*?)</table>";
MatchCollection Tables = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);