开发者社区> 问答> 正文

解析HTML字符串以提取值

<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>

我想e0f6bb5061864d63b59a18d8187eed21从前面提供的正文中提取值。为此,我使用正则表达式编写了代码:

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();
    }

有没有更好更好的方法来编写此代码?

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 13:54:05 442 0
1 条回答
写回答
取消 提交回答
  • 假设str给定了字符串,那么提取值的简单regexp应该可以工作

    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);
    }
    

    回答来源:Stack Overflow

    2020-03-24 13:54:28
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像