「java全局匹配」全局匹配算法
今天给各位分享java全局匹配的知识,其中也会对全局匹配算法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
sftp免密用java怎么调用
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.lang.ArrayUtils;
import com.huawei.bme.commons.om.log.DebugLog;
import com.huawei.bme.commons.om.log.LogFactory;
import com.huawei.icity.commons.constants.KeyConstant;
import com.huawei.icity.commons.constants.NumberConstant;
import com.huawei.icity.commons.exception.SPMException;
import com.huawei.icity.commons.log.IcityLogFactory;
import com.huawei.icity.commons.log.IcityRuntimeLog;
import com.huawei.icity.commons.sysconfig.StaticInitData;
import com.huawei.icity.commons.utils.StringTools;
import com.huawei.icity.omp.common.util.CommonTools;
import com.huawei.icity.omp.interfaces.hint.constant.TimetaskConstants;
import com.huawei.mdmc.bfm.cms.assist.common.domain.SingleTableModel;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelS;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
/**
* SFTP公共处理类 〈提供SFTP文件上传,下载,获取指定目录 下文件名集合, 获取指定目录 下文件集合等方法
*
* @author mKF75022
* @version iCity Manager V100R002 2012-7-3
* @since iCity Manager V100R002C01
*/
public class SFTPTool
{
/**
* 调测日志记录器。
*/
private static final DebugLog DEBUGGER = LogFactory.getDebugLog(SFTPTool.class);
/**
* 运行日志记录器。
*/
private static final IcityRuntimeLog RUNTIMELOGGER = IcityLogFactory
.getRuntimeLog(SFTPTool.class);
/**
* Sftp客户端对象
*/
private ChannelSftp sftp = null;
/**
* SFTP IP地址
*/
private String ip;
/**
* SFTP 端口
*/
private String port;
/**
* SFTP 用户名
*/
private String userName;
/**
* SFTP 密码
*/
private String password;
/**
* SFTP上传模式:BINARY
*/
// private static final int BINARY_FILE_TYPE = 2;
/**
*
* 获取实例
*
* @return SFTPTool newinstance实例
*
*/
public static SFTPTool getNewInstance()
{
return new SFTPTool();
}
/**
* 初始化连接参数
*
* @param sftpIP
* IP
* @param sftpPort
* 端口
* @param sftpUsername
* 用户名
* @param sftpPassword
* 密码
*/
public void init(String sftpIP, String sftpPort, String sftpUsername, String sftpPassword)
{
// 获取SFTP连接信息
this.ip = sftpIP;
this.port = sftpPort;
this.userName = sftpUsername;
this.password = sftpPassword;
}
/**
* 从SFTP将符合约定命名的文件都下载到本地 .
*
* @param sftpDir
* SFTP服务器文件存放路径
* @param locDir
* 本地文件存放路径
* @param regex
* 指定文件名的格式
* @param needBackup
* 是否需要文件备份(true:是;false:否)
* @param needFullMatch
* 是否要求全局匹配(true:是;false:否)
* @param deleteFtpFile
* the delete ftp file
* @return 下载到本地的文件列表
*/
public ListFile synSFTPFileToLocal(String sftpDir, String locDir, String regex,
boolean needBackup, boolean needFullMatch, boolean deleteFtpFile)
{
ListFile files = new ArrayListFile(KeyConstant.INITIAL_NUMBER);
try
{
this.connect(ip, Integer.parseInt(this.port), userName, password);
// 获得FTP上文件名称列表
ListString ftpFileNameList = this.listFiles(sftpDir, regex, needFullMatch);
File localFile = null;
int size = ftpFileNameList.size();
// 根据每个FTP文件名称创建本地文件。
for (int i = 0; i size; i++)
{
// 下载源文件
localFile = this.download(sftpDir, locDir, ftpFileNameList.get(i), deleteFtpFile);
if (localFile.exists())
{
files.add(localFile);
}
if (needBackup)
{
// 备份源文件生成默认备份文件路径(据请求文件路径,生成同级目录的备份文件夹绝对路径)
String parentDir = sftpDir.substring(NumberConstant.INT_0,
sftpDir.lastIndexOf("/") + 1);
String backupDir = parentDir + TimetaskConstants.DEFAULT_BACKUP_DIRNAME;
boolean bakExists = openDir(backupDir);
if (bakExists)
{
this.uploadFile(backupDir, localFile);
}
else
{
boolean parentExists = openDir(parentDir);
if (parentExists)
{
s;
this.uploadFile(backupDir, localFile);
}
else
{
DEBUGGER.error("sftp parentDir no exisits ");
}
}
}
}
}
catch (Exception e)
{
DEBUGGER.error("synSFTPFileToLocal Exception", e);
}
finally
{
this.disconnect();
}
return files;
}
/**
* 连接sftp服务器
*
* @param sftpip
* ip地址
* @param sftpport
* 端口
* @param sftpusername
* 用户名
* @param sftppassword
* 密码
* @return channelSftp
* @throws SPMException
*/
public ChannelSftp connect(String sftpip, int sftpport, String sftpusername, String sftppassword)
{
sftp = new ChannelSftp();
try
{
JSch jsch = new JSch();
jsch.getSession(sftpusername, sftpip, sftpport);
Session sshSession = jsch.getSession(sftpusername, sftpip, sftpport);
RUNTIMELOGGER.info("Session created");
sshSession.setPassword(sftppassword);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
// 设置超时时间为
sshSession.setTimeout(Integer.parseInt(StaticInitData.getFtpConnectTimeOut())
* NumberConstant.INT_1000);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
// 设置文件类型
// ftpClient.setFileType(BINARY_FILE_TYPE);
// 与防火墙相关
// ftpClient.enterLocalPassiveMode();
}
catch (JSchException e)
{
DEBUGGER.error("JSchException : {}", e);
}
return sftp;
}
// /**
// * 创建指定文件夹
// *
// * @param dirName
// * dirName
// */
// public void mkDir(String dirName)
// {
// try
// {
// s;
// }
// catch (SftpException e)
// {
// DEBUGGER.error("mkDir Exception : " + e);
// }
// }
/**
* 创建指定文件夹
*
* @param dirName
* dirName
*/
public void mkDir(String dirName)
{
String[] dirs = dirName.split("/");
try
{
String now = s;
for (int i = 0; i dirs.length; i++)
{
boolean dirExists = openDir(dirs[i]);
if (!dirExists)
{
s(dirs[i]);
s(dirs[i]);
}
}
s;
}
catch (SftpException e)
{
DEBUGGER.error("mkDir Exception : " + e);
}
}
/**
* 打开指定目录
*
* @param directory
* directory
* @return 是否打开目录
*/
public boolean openDir(String directory)
{
try
{
s;
return true;
}
catch (SftpException e)
{
DEBUGGER.error("openDir Exception : " + e);
return false;
}
}
java 正则表达式 包含字母数字特殊字符
String regEx="[A-Z,a-z,0-9,-]*"
boolean result=Pattern.compile(regEx).matcher(str).find();
java正则表达式 如何全局匹配
楼上有个说的很对,用while(m.find())。。。。。
具体就是:
while(m.find())
{
// 处理
String string = m.group();
}
// while中的find,是进行一次搜索,发现后即找到aab,进行循环体中处理,,然后再从上次找的的地方继续向后find。。。。。。。。。直到结束
正则表达式.+?是否包含换行符
其实.+?是既可以区配换行符也可以不区配换行符的。
这个和你是启用单行模式还是启用多行模式有关。
正则表达式对象模式仅有如下三种:
g (全文查找出现的所有 pattern)
i (忽略大小写)
m (多行查找)
1.单行模式(单行模式允许小数点(.)匹配包括换行符(\n)在内的任意字符)
开启单行模式:.可以匹配任意字符(包括换行符)
关闭单行模式:.只有匹配 非换行 的其它任意字符(.可匹配\r,即除了不匹配\n外的所有字符。)
2.多行模式
多行模式 影响 ^ 和 $ 的匹配单行模式 影响 . 的匹配
多行模式中必定包含 ^ 或 $ 或同时包含,否则即使加了 m,也没有任何意义
单行模式和多行模式是八杆子打不着的两个概念,只是因为正则发展的历史原因,造就了这样两个MS互斥的概念。
单行模式影响的是小数点“.”的匹配范围。
多行模式影响的是“^”和“$”的匹配范围。
3.全局匹配
至于后面几个概念,全局匹配,多行模式和贪婪模式之间也没有什么必然联系
全局匹配关闭,只匹配首次成功匹配项,全局匹配开启,匹配所有成功匹配项全局模式是一些脚本语言中才有的概念
在进行匹配时,关闭全局模式,类似于.NET中的Match方法,开启全局模式,类似于.NET中的Matches方法
在进行替换时,关闭全局模式,类似于Java中的replaceFirst,开启全局模式,类似于Java中的replaceAll
(在进行匹配时,关闭全局模式,类似于PHP中的preg_ match函数;开启全局模式,类似于PHP中的preg_ match_ all函数)
我不知道你使用的是那种编程语言,只能广泛举例了。
java全局匹配的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于全局匹配算法、java全局匹配的信息别忘了在本站进行查找喔。
发布于:2022-12-21,除非注明,否则均为
原创文章,转载请注明出处。