英文:
Parse HTML String for extracting a value
问题
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();
}
如果您希望从前面提供的代码中提取值 e0f6bb5061864d63b59a18d8187eed21
,可以使用以下代码进行提取。不过请注意,使用正则表达式来解析 HTML 或者其他标记语言并不是最佳的方法,因为它可能会很脆弱并且容易出错。更好的方法是使用专门的 HTML 解析库来处理这些情况。
英文:
I have <script> 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?
答案1
得分: 2
假设str
是给定的字符串,因此要提取值,简单的正则表达式应该可行。
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);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论