📄 regutils.java
字号:
package com.common.ubb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Perl5Substitution;
import org.apache.oro.text.regex.Util;
/**
* 专用于方便使用正则表达式的工具类
*
* @author magician
* @version 1.0
*/
public class RegUtils
{
//~ Static fields/initializers -----------------------------------------------------------------
private static final Log logger = LogFactory.getLog(RegUtils.class);
/**
* 基于ORO的正则分析
*/
private static final PatternCompiler compiler = new Perl5Compiler();
private static final PatternMatcher matcher = new Perl5Matcher();
//~ Constructors -------------------------------------------------------------------------------
private RegUtils()
{
}
//~ Methods ------------------------------------------------------------------------------------
public static String substitute(String[] patterns,String[] replace,String[] tag,String str,
int len)
{
org.apache.oro.text.regex.Pattern pattern = null;
try
{
//通过正则表达式分析给定字符串
for (int i = 0; i < len; i++)
{
//要包含相应的标签才会去匹配
if (str.indexOf(tag[i]) != -1)
{
if (logger.isDebugEnabled())
{
logger.debug("find pattern : " + patterns[i]);
}
pattern = compiler.compile(patterns[i]);
str = Util.substitute(matcher,pattern,
new Perl5Substitution(replace[i],
Perl5Substitution.INTERPOLATE_ALL),
str,Util.SUBSTITUTE_ALL);
}
}
}
catch (Exception e)
{
logger.error(e);
}
return str;
}
public static String substitute(String patter,String replace,String tag,String str)
{
org.apache.oro.text.regex.Pattern pattern = null;
try
{
//通过正则表达式分析给定字符串
if (str.indexOf(tag) != -1)
{
pattern = compiler.compile(patter);
str = Util.substitute(matcher,pattern,
new Perl5Substitution(replace,
Perl5Substitution.INTERPOLATE_ALL),str,
Util.SUBSTITUTE_ALL);
}
}
catch (Exception e)
{
logger.error(e);
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -