String上可使用正则表达式的操作,实际上是利用了java.util.regex.Pattern与java.util.regex.Matcher的功能。当调用String的matches()方法时,实际上是调用Pattern的静态方法matches(),这个方法会返回boolean值,表示字符串是否符合正则表达式。
如果想要将正则表达式视为一个对象来重复使用,可以使用Pattern的静态方法compile()进行编译。compile()方法会返回一个Pattern的实例,这个实例代表正则表达式,之后就可以重复使用Pattern实例的matcher()方法来返回一个Matcher的实例,代表符合正则式的实例,这个实例上有一些寻找符合正则式条件的方法可供操作。
function ajaxExportExcel(formName){
var type = "${isBranch}";
//if(type=="0"){
// Ext.MessageBox.alert("警告", "没有数据!");
// return;
//}
var agentName = "agentName:" + Ext.getCmp("agentName").getValue();
var system = "system:" + Ext.getCmp("system_id").getValue();
if (Ext.getCmp("system_id").getRawValue()=='') {
system = "system:";
}
var officeNo = "officeNo:" + Ext.getCmp("officeNo").getValue();
var status = "status:" + Ext.getCmp("status_id").getValue();
if (Ext.getCmp("status_id").getRawValue()=='') {
status = "status:";
}
var startTime = "startTime:" + Ext.getCmp("startTime_index_1").getRawValue();
var endTime = "endTime:" + Ext.getCmp("startTime_index_2").getRawValue();
var zData = "zData:" + Ext.getCmp("zData").getValue();
var params = agentName + "_@_" + system + "_@_" + officeNo + "_@_"
+ status + "_@_"+ startTime + "_@_" + endTime + "_@_" + zData;
var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"请稍候..."});
myMask.show();
Ext.Ajax.request({
url: "./report!exportqueryHistorysReport.action",
timeout: 900000,
method: 'post',
params: {queryInfo: params},
success:function(action){
var response = action.responseText;
if(response.indexOf('file:')==-1){
myMask.hide();
Ext.MessageBox.show({
title: "导出报表失败",
msg: response,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.INFO
});
}else{
var reportFileName = response.substring(5);
var newWindow = new Ext.Window({
id: 'winExportReport',
title: this.title,
width: 850,
layout: 'fit',
collapsible: true,
height: this.height,
frame: true,
modal: true,
html: '<iframe id="iframe1" src="../report/download?filePath='+reportFileName+'" width="100%" height="500"/>'
});
newWindow.addListener("show", function(){delayCloseWindow(myMask);});
newWindow.addListener("close", function(){myMask.hide();});
newWindow.show();
}
},
failure: function(action){
var response = action.responseText;
myMask.hide();
Ext.MessageBox.show({
title: "导出报表失败",
msg: response,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.INFO
});
}
});
}
private static Pattern configSummaryQueryParameterPatternhy =
Pattern.compile("^agentName:(.*)_@_system:(.*)_@_officeNo:(.*)_@_status:(.*)_@_startTime:(.*)_@_endTime:(.*)_@_zData:(.*)$");
public String exportqueryHistorysReport()throws Exception{
//数据渲染器选择标记
ServletActionContext.getRequest().setAttribute(
com.travelsky.bravo.core.ui.Constants.AJAX_HANDLE_KEY,
com.travelsky.bravo.core.ui.Constants.AJAX_HANDLE_VALUE);
try{
String queryInfo = ServletActionContext.getRequest().getParameter("queryInfo").trim();
Matcher matcher = configSummaryQueryParameterPatternhy.matcher(queryInfo);
if (!matcher.find()) {
throw new RuntimeException("wrong parameters:" + queryInfo);
}
SecUser currentUser = RequestHandler.getContextRequestHandler().getCurrentUser();
String islocaluse=currentUser.getBranchId().getIslocaluse();
String fileName = reportManager.generateConfigHistorysReport(
StringUtils.trim(matcher.group(1)),
StringUtils.trim(matcher.group(2)),
StringUtils.trim(matcher.group(3)),
StringUtils.trim(matcher.group(4)),
StringUtils.trim(matcher.group(5)),
StringUtils.trim(matcher.group(6)),
StringUtils.trim(matcher.group(7)),islocaluse);
ServletActionContext.getRequest().setAttribute(
Constants.AJAX_HANDLE_STATUS, "Y");
ServletActionContext.getRequest().setAttribute(
Constants.AJAX_HANDLE_MSG, "file:" + fileName);
}catch(Exception e){
logger.error(e.getMessage(), e);
ServletActionContext.getRequest().setAttribute(
Constants.FORM_AJAX_SUBMIT_STATUS,
Constants.FORM_AJAX_SUBMIT_FAILURE);
ServletActionContext.getRequest().setAttribute(
Constants.FORM_AJAX_SUBMIT_MSG,
e.getMessage());
}
return JSON_DATA_RENDER_CHAIN;
}
本文介绍如何在Java中使用正则表达式处理字符串,包括Pattern和Matcher类的基本使用方法,以及通过实例演示如何编译正则表达式并进行匹配。
703

被折叠的 条评论
为什么被折叠?



