1

I have tag inside the HTML document provide below:

<script type="text/javascript">
  var ReportViewer1 = new ReportViewer('ReportViewer1', 'ReportViewer1_ReportToolbar', 'ReportViewer1_ReportArea_WaitControl', 'ReportViewer1_ReportArea_ReportCell', 'ReportViewer1_ReportArea_PreviewFrame', 'ReportViewer1_ParametersAreaCell', 'ReportViewer1_ReportArea_ErrorControl', 'ReportViewer1_ReportArea_ErrorLabel', 'ReportViewer1_CP', '/app/Telerik.ReportViewer.axd', 'e0f6bb5061864d63b59a18d8187eed21', 'Percent', '100', '', 'ReportViewer1_EditorPlaceholder', 'ReportViewer1_CalendarFrame', 'ReportViewer1_ReportArea_DocumentMapCell',
  {
    CurrentPageToolTip: 'STR_TELERIK_MSG_CUR_PAGE_TOOL_TIP',
    ExportButtonText: 'Export',
    ExportToolTip: 'Export',
    ExportSelectFormatText: 'Export to the selected format',
    FirstPageToolTip: 'First page',
    LabelOf: 'of',
    LastPageToolTip: 'Last Page',
    ProcessingReportMessage: 'Generating report...',
    NoPageToDisplay: 'No page to display.',
    NextPageToolTip: 'Next page',
    ParametersToolTip: 'Click to close parameters area|Click to open parameters area',
    DocumentMapToolTip: 'Hide document map|Show document map',
    PreviousPageToolTip: 'Previous page',
    TogglePageLayoutToolTip: 'Switch to interactive view|Switch to print preview',
    SessionHasExpiredError: 'Session has expired.',
    SessionHasExpiredMessage: 'Please, refresh the page.',
    PrintToolTip: 'Print',
    RefreshToolTip: 'Refresh',
    NavigateBackToolTip: 'Navigate back',
    NavigateForwardToolTip: 'Navigate forward',
    ReportParametersSelectAllText: '<select all>',
    ReportParametersSelectAValueText: '<select a value>',
    ReportParametersInvalidValueText: 'Invalid value.',
    ReportParametersNoValueText: 'Value required.',
    ReportParametersNullText: 'NULL',
    ReportParametersPreviewButtonText: 'Preview',
    ReportParametersFalseValueLabel: 'False',
    ReportParametersInputDataError: 'Missing or invalid parameter value. Please input valid data for all parameters.',
    ReportParametersTrueValueLabel: 'True',
    MissingReportSource: 'The source of the report definition has not been specified.',
    ZoomToPageWidth: 'Page Width',
    ZoomToWholePage: 'Full Page'
}, 'ReportViewer1_ReportArea_ReportArea', 'ReportViewer1_ReportArea_SplitterCell', 'ReportViewer1_ReportArea_DocumentMapCell', true, true, 'PDF', 'ReportViewer1_RSID', true);
    </script>

I would like to extract the value e0f6bb5061864d63b59a18d8187eed21 from the body provided earlier. I wrote the code using regex for the purpose:

final String BEFORE_INSTANCE_ID = "/app/Telerik.ReportViewer.axd";
final String AFTER_INSTANCE_ID = "Percent";

Pattern pattern = Pattern.compile("(" + BEFORE_INSTANCE_ID + ")(.*?)(" + AFTER_INSTANCE_ID + ")");
        Matcher matcher = pattern.matcher(body);


    String instanceId = null;

    while (matcher.find()) {

        String temp = matcher.group(0);
        instanceId = StringUtils.substringBetween(temp, BEFORE_INSTANCE_ID, AFTER_INSTANCE_ID).replaceAll("[,;'\\s]", "").trim();
    }

Is there a better and nicer way to code this?

3
  • JSoup may be the best API for you Commented Mar 15, 2020 at 14:03
  • I asked the question earlier and someone answered using the JSoup. The code doesn't work and I was also not able to correct it. But that is better way to go I believe. Commented Mar 15, 2020 at 14:04
  • Getting it via javasript might be the easiest and hackiest solution. see this Commented Mar 15, 2020 at 14:07

1 Answer 1

2

Assume str is given string, so to extract the value simple regexp should work

Pattern pattern = Pattern.compile(",\\s*'([0-9a-f]{32})'\\s*,", Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
String result = null;
if(matcher.find()) {
    result = matcher.group(1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.