📄 regexp.java
字号:
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回真,否则返回假 如:
* Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp) 返回真
*/
public static boolean isHardRegexpValidate(String source, String regexp) {
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例大小大小写敏感的正规表达式模板
Pattern hardPattern = compiler.compile(regexp);
// 返回批配结果
return matcher.contains(source, hardPattern);
} catch (MalformedPatternException e) {
e.printStackTrace();
}
return false;
}
/**
* 不区分大小写的正规表达式批配
*
* @param source
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回真,否则返回假
* Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp) 返回真
*/
public static boolean isSoftRegexpValidate(String source, String regexp) {
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例不区分大小写的正规表达式模板
Pattern softPattern = compiler.compile(regexp,
Perl5Compiler.CASE_INSENSITIVE_MASK);
// 返回批配验证值
return matcher.contains(source, softPattern);
} catch (MalformedPatternException e) {
e.printStackTrace();
}
return false;
}
/**
* 返回许要的批配结果集(大小写敏感的正规表达式批配)
*
* @param source
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回许要的批配结果集,否则返回 "null"关键字 <br>
* 如 : MatchResult matchResult =
* getHardRegexpMatchResult("http://www.suncer.com:8080/index.html?login=true",Regexp.url_regexp)
* 得到取出的匹配值为 matchResult.group(0)=
* http://www.suncer.com:8080/index.html?login=true
* matchResult.group(1) = http matchResult.group(2) = www.suncer.com
* matchResult.group(3) = :8080 matchResult.group(4) =
* /index.html?login=true
*
*/
public static MatchResult getHardRegexpMatchResult(String source,
String regexp) {
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例大小大小写敏感的正规表达式模板
Pattern hardPattern = compiler.compile(regexp);
// 如果批配结果正确,返回取出的批配结果
if (matcher.contains(source, hardPattern)) {
// MatchResult result=matcher.getMatch();
return matcher.getMatch();
}
} catch (MalformedPatternException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回许要的批配结果集(不区分大小写的正规表达式批配)
*
* @param source
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回许要的批配结果集,否则返回 "null"关键字 如 : MatchResult matchResult =
* getHardRegexpMatchResult("http://www.suncer.com:8080/index.html?login=true",Regexp.url_regexp)
* 得到取出的匹配值为 matchResult.group(0)=
* http://www.suncer.com:8080/index.html?login=true
* matchResult.group(1) = http matchResult.group(2) = www.suncer.com
* matchResult.group(3) = :8080 matchResult.group(4) =
* /index.html?login=true
*/
public static MatchResult getSoftRegexpMatchResult(String source,
String regexp) {
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例不区分大小写的正规表达式模板
Pattern softPattern = compiler.compile(regexp,
Perl5Compiler.CASE_INSENSITIVE_MASK);
// 如果批配结果正确,返回取出的批配结果
if (matcher.contains(source, softPattern)) {
// MatchResult result=matcher.getMatch();
return matcher.getMatch();
}
} catch (MalformedPatternException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回许要的批配结果集(大小写敏感的正规表达式批配)
*
* @param source
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回许要的批配结果集,{@link #getHardRegexpMatchResult(String, String) 使用方法参见getHardRegexpMatchResult(String, String)}
*/
public static ArrayList getHardRegexpArray(String source, String regexp) {
List tempList = new ArrayList();
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 实例大小大小写敏感的正规表达式模板
Pattern hardPattern = compiler.compile(regexp);
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 如果批配结果正确,返回取出的批配结果
if (matcher.contains(source, hardPattern)) {
// 存放取出值的正规表达式比较批配结果对象
MatchResult matchResult = matcher.getMatch();
for (int i = 0; i < matchResult.length()
&& matchResult.group(i) != null; i++) {
tempList.add(i, matchResult.group(i));
}
}
} catch (MalformedPatternException e) {
e.printStackTrace();
}
return (ArrayList) tempList;
}
/**
* 返回许要的批配结果集(不区分大小写的正规表达式批配)
*
* @param source
* 批配的源字符串
*
* @param regexp
* 批配的正规表达式
*
* @return 如果源字符串符合要求返回许要的批配结果集{@link #getHardRegexpMatchResult(String, String) 使用方法参见getHardRegexpMatchResult(String, String)}
*/
public static ArrayList getSoftRegexpArray(String source, String regexp) {
List tempList = new ArrayList();
try {
// 用于定义正规表达式对象模板类型
PatternCompiler compiler = new Perl5Compiler();
// 正规表达式比较批配对象
PatternMatcher matcher = new Perl5Matcher();
// 实例不区分大小写的正规表达式模板
Pattern softPattern = compiler.compile(regexp,
Perl5Compiler.CASE_INSENSITIVE_MASK);
if (matcher.contains(source, softPattern)) {
// 如果批配结果正确,返回取出的批配结果
MatchResult matchResult = matcher.getMatch();
for (int i = 0; i < matchResult.length()
&& matchResult.group(i) != null; i++) {
tempList.add(i, matchResult.group(i));
}
}
} catch (MalformedPatternException e) {
e.printStackTrace();
}
return (ArrayList) tempList;
}
/**
* <pre>
* 得到指定分隔符中间的字符串的集合,
* 说明: 1.分隔符为 (),[],{},<> 中的一组
* 2.得到的集合以 ASCII 的升序排列
* 如 String originStr="((([a]+[b])/[c])-24)+[d]";
* 则 getStrBetweenSeparator(originStr,"[","]"));
* 返回结果集合有四个元素 [a, b, c, d],
* 以 ASCII 的升序排列
* </pre>
*
* @param originStr
* 要提取的源字符串
* @param leftSeparator
* 左边的分隔符
* @param rightSeparator
* 右边的分隔符
* @return 指定分隔符中间的字符串的集合
*/
public static Set getBetweenSeparatorStr(final String originStr,
final char leftSeparator, final char rightSeparator) {
Set variableSet = new TreeSet();
// if(StringHelp.isEmpty(originStr))
if (originStr == null) {
return variableSet;
}
String sTempArray[] = originStr.split("(\\" + leftSeparator + ")");
for (int i = 1; i < sTempArray.length; i++) {
int endPosition = sTempArray[i].indexOf(rightSeparator);
String sTempVariable = sTempArray[i].substring(0, endPosition);
variableSet.add(sTempVariable);
}
return variableSet;
}
public static void main(String a[]) {
// PatternCompiler compiler = new Perl5Compiler();
// PatternMatcher matcher = new Perl5Matcher();
// MatchResult matchResult=matcher.getMatch();
// String email = "wuzhi2000@hotmail.com.cn";
// String email_regexp = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
// String url = "http://www";
// String url_regexp =
// "^(?:http|https|ftp)://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";//
// 匹配url
// String date = "1111-1-9";
// String date_regexp =
// "^[1-9]{1}[0-9]{0,3}[-\\s][0-9]{1,2}[-\\s][0-9]{1,2}$";// 匹配日期
// String phone = "010-1234567";
// String phone_regexp =
// "^(?:0[0-9]{2,4}[-\\s]{1}|\\(0[0-9]{2,4}\\))[0-9]{6,8}$|^[0-9]{6,8}$";//
// 匹配电话
// String icon="/he//fff/aaaq34.gif";
// String icon_regexp =
// "^(/{0,1}\\w){1,}\\.(gif|dmp|png)$|^\\w{1,}\\.(gif|dmp|png)$";
// String name="\"^";
// String number="023";
// String pic ="forum/head_icons/anoymous20050428125334.jpg";
String mobile = "14400000001";
String mobile_regexp = "^(1[3,4,5,6][0,1,2,3,4,5,6,7,8,9]\\d{8})$";
System.out.println(Regexp.isSoftRegexpValidate(mobile, mobile_regexp));
/*
* try {
*
*
* PatternCompiler compiler = new Perl5Compiler(); PatternMatcher
* matcher = new Perl5Matcher(); MatchResult
* matchResult=matcher.getMatch();
*
* //一个提取html标签属性的例子 String regexpForFontTag="<\\s*font\\s+([^>]*)\\s*>";
* String regexpForFontAttrib="([a-z]+)\\s*=\\s*\"([^\"]+)\"";
*
* String html=" <font face=\"Arial, serif\" size=\"+2\"
* color=\"red\">"; System.out.println(regexpForFontTag);
* System.out.println(regexpForFontAttrib); System.out.println(html);
*
* Pattern
* p1=compiler.compile(regexpForFontTag,Perl5Compiler.CASE_INSENSITIVE_MASK);
* Pattern
* p2=compiler.compile(regexpForFontAttrib,Perl5Compiler.CASE_INSENSITIVE_MASK);
* System.out.println(matcher.contains(html,p1));
* if(matcher.contains(html,p1)) { MatchResult
* result=matcher.getMatch(); System.out.println(result.group(1));
* String attribs=result.group(1);
*
* PatternMatcherInput input =new PatternMatcherInput(attribs);
* System.out.println(matcher.contains(input,p2));
* while(matcher.contains(input,p2)) { result=matcher.getMatch();
* System.out.println(result.group(1)+" : "+result.group(2)); } } }
* catch(MalformedPatternException e) { e.printStackTrace(); }
*/
/*
* //一个提取http的例子 String try { PatternCompiler compiler = new
* Perl5Compiler(); PatternMatcher matcher = new Perl5Matcher();
* MatchResult matchResult=matcher.getMatch(); String
* http="http://www.suncer.com:8080/index.html?login=true"; String
* http_regexp="(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)"; Pattern
* p1=compiler.compile(http_regexp,Perl5Compiler.CASE_INSENSITIVE_MASK);
* System.out.println(matcher.contains(http,p1));
* if(matcher.contains(http,p1)) { MatchResult
* result=matcher.getMatch(); System.out.println(result.group(1));
* String attribs=result.group(1);
*
* for( int i=0;i <result.length() && result.group(i)!=null; i++) {
* System.out.println(i+" : "+result.group(i)); } } }
* catch(MalformedPatternException e) { e.printStackTrace(); }
*/
// 一个提取字符串中的中括号 [ ] 包含的字符的例子
// String expression = "((([a]+[b])/[c])-24)+[d]";
// System.out.println( getBetweenSeparatorStr(expression,'[',']'));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -