0

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);
5
  • 1
    Dont parse html with regex Feb 15, 2018 at 14:46
  • Making a second comment because I want to leave something constructive & I cant edit my other comment, try using Apex's XML Support to read this html instead. Feb 15, 2018 at 14:53
  • @battery.cord Is there any sample in apex about how to parse using XML?
    – Ean
    Feb 15, 2018 at 14:53
  • 1
    The streams example is a little more relevant, this is old but seems worth looking at, this question also. Feb 15, 2018 at 14:58
  • @battery.cord Thanks for providing the url of Apex's xml support.
    – Ean
    Feb 15, 2018 at 14:58

1 Answer 1

3

Calling myPattern.matcher() just creates a Matcher object you can use to match against the string. You need to call methods on the Matcher to actually get back match data. For example, something like

while (myMatcher.find()) {
    String thisGroup = myMatcher.group();
    // do something with this group, add to a List, etc.
}

would allow you to iterate through the matched tables.

Differences that you observe in behavior between hard-coded and web-sourced HTML are very case-specific, being dependent on the specific HTML and your regex. This would require a specific reproducible example to attempt to address.

3
  • I tried adding '(?m)(?i)(?s)' to the pattern and now i am getting the desired output.
    – Ean
    Feb 15, 2018 at 15:01
  • @Ean You can combine flags. Use (?mis) instead. Although m and s conflict, so only s is applied. Use (?is).
    – Adrian Larson
    Feb 15, 2018 at 17:34
  • @Adrian Larson Thanks Adrian. I have combined the flags now.
    – Ean
    Feb 16, 2018 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .